﻿angular.module('app.configurationsControllers', [])

    .controller('configurationsControllers', ['$scope', 'configurationsService', 'localStorageService', 'tokenService', 'configurationStepsService', '$filter', '$interval', '$location',
        function ($scope, configurationsService, localStorageService, tokenService, configurationStepsService, $filter, $interval, $location) {

            if ($scope.applicationData.companyIsConfig) {
                $location.path('/dashboard');
            }

            $scope.configurationData = {
                loading: true,
                titlePage: null,
                step: 0,
                pageUrl: null
            };

            $scope.configurationSteps = [];

            $scope.authData = localStorageService.get('authorizationData');

            $scope.getAuthorization = function () {
                configurationsService.getAuthentificationConfig().then(function (results) {
                    var dataParse = $.parseJSON(results.data.data);
                    dataParse.claims = $.parseJSON(dataParse.claims);
                    localStorageService.set('authorizationData', { userName: dataParse.claims.nameidentifier, userData: dataParse });
                    $scope.loadServices();
                });
            };

            $scope.loadServices = function () {
                $scope.configurationData.loading = true;
                configurationsService.getConfigStep().then(function (results) {
                    $scope.configurationData.loading = false;
                    $scope.configurationData.step = Number(results.data);
                    $scope.initializeConfig();
                });

            };

            $scope.initializeConfig = function () {
                configurationStepsService.GetconfigurationSteps().then(function (results) {
                    $scope.configurationSteps = results.data;
                    $scope.scrollDiv();
                    var configurationDataStep = $scope.configurationData.step;
                    $.extend($scope.configurationData, $filter("filter")($scope.configurationSteps, { step: configurationDataStep }, true)[0]);
                });
            };

            $scope.scrollDiv = function () {
                var scr = 20 * $scope.configurationData.step;
                var intervalScroll = $interval(function () {
                    if ($("#menuOptionsLeft div").length === $scope.configurationSteps.length) {
                        $("#menuOptionsLeft").scrollTop(scr);
                        $interval.cancel(intervalScroll);
                    }
                });


            };

            $scope.reloadFromChild = function () {
                $scope.configurationData.loading = true;
                setTimeout(function () {
                    $scope.loadServices();
                }, 500);
            };

            $scope.previousPage = function (id) {
                $scope.configurationData.loading = true;

                if ($scope.AppDataConfig.HR_COMPANY == "false" && $scope.configurationData.step == 5) {
                    $scope.config = { name: "CONFIG_STEP", value: Number($scope.configurationData.step) - 3 };
                } else {
                    $scope.config = { name: "CONFIG_STEP", value: Number($scope.configurationData.step) - 1 };
                }

                setTimeout(function () {
                    configurationsService.saveConfigByName($scope.config).then(function (results) {
                        $scope.loadServices();
                    });
                }, 500);
            };

            if ($scope.authData) {
                tokenService.IsAuthenticated().then(function (results) {
                    if (results.data) {
                        if ($scope.authData.userName === "k9handler") {
                            $scope.showMessageSmart("<i class='fa fa-sign-out txt-color-orangeDark'></i> Logout <span class='txt-color-orangeDark'><strong></strong></span> ?", 'This user can not perform any action, please log in with an administrator user', null, "[Ok]");
                            $scope.logout();
                        } else {
                            $scope.loadServices();
                        }
                    } else {
                        $location.path('/login');
                    }
                });
            } else {
                $location.path('/login');
            }

            $scope.selectStep = function (data) {
                $scope.configurationData.loading = true;
                $scope.configurationData.step = data;
                setTimeout(function () {
                    configurationsService.saveConfigDataWizard({ CONFIG_STEP: data }).then(function (results) {
                        $scope.loadServices();
                    });
                }, 500);
            };

            $scope.importWizard = function () {
                $("#importDialog").modal({
                    backdrop: 'static',
                    keyboard: false
                }).modal("show");

                $scope.$broadcast('dialogImportOpen');
            };

            $scope.expressSettings = function () {
                $("#expressSettings").modal("show");
            };

            $scope.removeAllConfirm = function () {
                $scope.showMessageSmart("<i class='fa fa-info'></i> Information", "You are about to remove all the records, this action cannot be undone!", $scope.runRemoveAllConfirm);
            };

            $scope.runRemoveAllConfirm = function () {
                $scope.$broadcast('delete-all');
            };

            $scope.userLogout = function () {
                $scope.showMessageSmart("<i class='fa fa-sign-out txt-color-orangeDark'></i> Logout <span class='txt-color-orangeDark'><strong></strong></span> ?", '', $scope.logout);
            };

        }])

    .controller('companyInfoController', ['$scope', 'configurationsService', '$interval', 'configsService',
        function ($scope, configurationsService, $interval, configsService) {

            $scope.configuration = {};
            $scope.uploadingImage = false;
            $scope.cpyImageData = null;

            $scope.loadServices = function () {
                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        $scope.configuration[data.name] = data.value;
                    });
                });

                configsService.getTimeZonesInfo().then(function (results) {
                    $scope.timeZones = results.data;
                });

                $scope.binNgChangeImg();
            };

            $scope.binNgChangeImg = function () {
                var intervalImgBindChange = $interval(function () {
                    if ($("#uploadFile").length > 0) {
                        $("#uploadFile").bind("change", function () {
                            $scope.uploadFile(this);
                        });
                        $interval.cancel(intervalImgBindChange);
                    }
                });
            };

            $scope.saveConfig = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.configurationData.loading = true;
                    $scope.configuration.CONFIG_STEP = "1";
                    configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                        $scope.reloadFromChild();
                    });
                }
            };

            $scope.uploadFile = function (data) {
                $scope.uploadingImage = true;
                $scope.configuration.cpyImage = data.files[0].name;
                $scope.cpyImageData = new FormData();
                $scope.cpyImageData.append("file" + 0, data.files[0]);
                setTimeout(function () {
                    $scope.$apply();
                }, 200);

                configurationsService.imageUpload($scope.cpyImageData).then(function (results) {
                    $scope.uploadingImage = false;
                });
            };

            $scope.loadServices();


        }])

    .controller('fiscalYearController', ['$scope', 'configurationsService', '$interval', 'currenciesService', 'fiscalPeriodsService',
        function ($scope, configurationsService, $interval, currenciesService, fiscalPeriodsService) {

            $scope.configuration = {
                fiscalPeriod: {
                    generateMonths: true
                }
            };

            $scope.fiscalsMonths = [
                { id: "1", name: "January" },
                { id: "2", name: "February" },
                { id: "3", name: "March" },
                { id: "4", name: "April" },
                { id: "5", name: "May" },
                { id: "6", name: "June" },
                { id: "7", name: "July" },
                { id: "8", name: "August" },
                { id: "9", name: "September" },
                { id: "10", name: "October" },
                { id: "11", name: "November" },
                { id: "12", name: "December" }
            ];

            $scope.yesNoValues = [
                { id: "0", name: "No" },
                { id: "1", name: "Yes" }
            ];

            $scope.profitMethod = [
                { id: "C", name: "Based on Cost" },
                { id: "P", name: "Based on Price" }
            ];

            $scope.currencies = [];

            $scope.loadServices = function () {
                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        $scope.configuration[data.name] = data.value;
                    });
                });

                fiscalPeriodsService.getFiscalPeriodWizard().then(function (results) {
                    $.extend($scope.configuration.fiscalPeriod, results.data);
                });

                currenciesService.getCurrencies().then(function (results) {
                    $scope.currencies = results.data;
                    var intervalCurrencyValue = $interval(function () {
                        if ($scope.configuration['SYSTEM_CURRENCY']) {
                            $scope.configuration['SYSTEM_CURRENCY'] = Number($scope.configuration['SYSTEM_CURRENCY']);
                            $interval.cancel(intervalCurrencyValue);
                        }
                    }, 100);
                });
            };

            $scope.saveConfig = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.configurationData.loading = true;
                    $scope.configuration.CONFIG_STEP = "2";
                    configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                        $scope.reloadFromChild();
                    });
                }
            };


            $scope.loadServices();

        }])

    .controller('emailConfigController', ['$scope', 'configurationsService',
        function ($scope, configurationsService) {

            $scope.configuration = {};
            $scope.checkingEmail = false;
            $scope.alertMessage = {
                display: false,
                text: ""
            };

            $scope.smtpencryption = [
                { id: "TLS", name: "Use TLS" },
                { id: "", name: "No Use TLS" }
            ];

            $scope.loadServices = function () {
                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        $scope.configuration[data.name] = data.value;
                        if (data.name === "USE_SYTEM_EMAIL") {
                            $scope.configuration[data.name] = (data.value !== "False");
                        }
                        if (data.name === "EMAIL_CREDENTIAL_PASSWORD") {
                            $scope.configuration["EMAIL_CREDENTIAL_PASSWORD_CONFIRM"] = data.value;
                        }
                    });
                });
            };

            $scope.saveConfig = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.configurationData.loading = true;
                    if ($scope.AppDataConfig.HR_COMPANY == "false") {
                        $scope.configuration.CONFIG_STEP = "5";
                    } else {
                        $scope.configuration.CONFIG_STEP = "3";
                    }

                    configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                        $scope.reloadFromChild();
                    });
                }
            };

            $scope.loadServices();


        }])

    .controller('userGroupConfigController', ['$scope', 'usersGroupsService', '$filter', 'configurationsService',
        function ($scope, usersGroupsService, $filter, configurationsService) {

            $scope.usersGroups = [];
            $scope.savingData = false;

            $scope.userGroup = {};

            $scope.modalData = {};

            $scope.saveGroup = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.savingData = true;
                    if ($scope.userGroup.id) {
                        usersGroupsService.putusersGroups($scope.userGroup).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            } else {
                                $scope.loadServices();
                                $("#addUserGroup").modal("hide");
                            }
                            $scope.savingData = false;
                        });
                    } else {
                        delete $scope.userGroup.id;
                        usersGroupsService.postusersGroups($scope.userGroup).then(function (results) {
                            if (!results.data.error) {
                                $scope.usersGroups.push($scope.userGroup);
                                $scope.loadServices();
                                $("#addUserGroup").modal("hide");
                                $scope.showMessageG("Information", "User Group created", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            }

                            $scope.savingData = false;
                        });
                    }
                }
            };

            $scope.deleteGroup = function () {
                $scope.savingData = true;
                usersGroupsService.DeleteAspNetUsersGroup($scope.userGroup.id).then(function (results) {
                    $scope.loadServices();
                    $("#addUserGroup").modal("hide");
                    $scope.savingData = false;
                    if (results.data) {
                        $scope.showMessageG("Information", results.data, 2);
                    }
                });
            };

            $scope.loadServices = function () {
                usersGroupsService.getusersGroups().then(function (results) {
                    $scope.usersGroups = results.data;
                });
            };

            $scope.addUserGroup = function (index) {
                if (index) {
                    $.extend($scope.userGroup, $filter('filter')($scope.usersGroups, { id: index }, true)[0]);
                    $scope.modalData.titleModal = "Edit User Group";
                } else {
                    $scope.userGroup = {
                        id: null,
                        name: null,
                        inactive: false,
                        administrator: false
                    };
                    $scope.modalData.titleModal = "Add User Group";
                }
                $("#addUserGroup").modal("show");
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 4
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                usersGroupsService.removeAllGroups().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('userConfigController', ['$scope', 'usersService', '$filter', 'configurationsService', 'usersGroupsService',
        function ($scope, usersService, $filter, configurationsService, usersGroupsService) {

            $scope.users = [];
            $scope.usersGroups = [];
            $scope.savingData = false;

            $scope.user = {
                Id: null,
                EmailConfirmed: false,
                PhoneNumberConfirmed: false,
                TwoFactorEnabled: false,
                LockoutEnabled: false,
                AccessFailedCount: 0,
                UserName: null,
                active: true
            };

            $scope.modalData = {};

            $scope.loadServices = function () {
                $scope.loadUsers();

                usersGroupsService.getusersGroups().then(function (results) {
                    $scope.usersGroups = results.data;
                });
            };

            $scope.loadUsers = function () {
                usersService.GetUsersDetailed().then(function (results) {
                    $scope.users = results.data;
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 5
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.addUser = function (index) {
                if (index) {
                    $scope.modalData.titleModal = "Edit User";
                    $.extend($scope.user, $filter('filter')($scope.users, { Id: index })[0]);
                } else {
                    $scope.modalData.titleModal = "Add User";
                    $scope.user = {
                        Id: null,
                        EmailConfirmed: false,
                        PhoneNumberConfirmed: false,
                        TwoFactorEnabled: false,
                        LockoutEnabled: false,
                        AccessFailedCount: 0,
                        UserName: null
                    };
                }

                $("#addUser").modal("show");
            };

            $scope.deleteUser = function () {
                usersService.DeleteAspNetUser($scope.user.Id).then(function (results) {
                    $scope.loadUsers();
                    $("#addUser").modal("hide");
                    if (results.data) {
                        $scope.showMessageG("Information", results.data, 2);
                    }
                });
            };

            $scope.saveUser = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.user.Id) {
                        usersService.putusers($scope.user).then(function (results) {
                            if (results.data) {
                                $scope.showMessageG("Information", results.data, 2);
                            } else {
                                $scope.loadUsers();
                                $("#addUser").modal("hide");
                            }
                        });
                    } else {
                        usersService.addUser($scope.user).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadUsers();
                                $("#addUser").modal("hide");
                            } else {

                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                               // $scope.showMessageG("Information", "The username is unique, the username already exist!", 2);
                            }
                        });
                    }
                }
            };

            $scope.loadServices();

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                usersService.removeAllUsers().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('balanceSheetHeadingConfigController', ['$scope', '$filter', 'configurationsService', 'coaBalSheetHdService', 'coaBalSheetHdSctService',
        function ($scope, $filter, configurationsService, coaBalSheetHdService, coaBalSheetHdSctService) {

            $scope.balanceSheetsHeadings = [];
            $scope.balanceSheetsHeadingSections = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.balanceSheetsHeadings.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.balanceSheetsHeadings.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.balanceSheetsHeadings.length;
            };

            $scope.balanceSheetsHeading = {
                id: null,
                heading: null,
                section: null,
                inactive: false
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Balance Sheet Heading";
                    $scope.balanceSheetsHeading = $.extend({}, $filter('filter')($scope.balanceSheetsHeadings, { id: ind }, true)[0]);
                    $scope.balanceSheetsHeading.section = Number($scope.balanceSheetsHeading.section);
                } else {
                    $scope.modalData.titleModal = "Add Balance Sheet Heading";
                    $scope.balanceSheetsHeading = {
                        id: null,
                        heading: null,
                        section: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.loadServices = function () {
                $scope.loadBalanceSheet();
                coaBalSheetHdSctService.GetchartOfAccountsBalanceSheetHeadingsSections().then(function (results) {
                    $scope.balanceSheetsHeadingSections = results.data;
                });
                configurationsService.Getconfig().then(function (results) {
                    $scope.configuration = {};
                    results.data.forEach(function (data) {
                        $scope.configuration[data.name] = data.value;
                    });
                });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.balanceSheetsHeading.id) {
                        coaBalSheetHdService.putCOABalSheetHd($scope.balanceSheetsHeading).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadBalanceSheet();
                                $("#addItem").modal("hide");
                            }
                        });
                    } else {
                        delete $scope.balanceSheetsHeading.id;
                        coaBalSheetHdService.postCOABalSheetHd($scope.balanceSheetsHeading).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadBalanceSheet();
                                $("#addItem").modal("hide");
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.loadBalanceSheet = function () {
                coaBalSheetHdService.getcoaBalanceSheetHeadings(112,100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.balanceSheetsHeadings = results.data[1];
                    $scope.pageChanged();
                });
            };

            $scope.expressAccountSettings = function () {
                $scope.configuration = {
                    CONFIG_STEP: 13
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                coaBalSheetHdService.deletechartOfAccountsBalanceSheetHeadings($scope.balanceSheetsHeading.id).then(function () {
                    $scope.configuration.USE_DEFAULT_BSH = 0;
                    configurationsService.saveConfigDataWizard($scope.configuration);
                    $scope.loadBalanceSheet();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.configuration.USE_DEFAULT_BSH = 0;
                configurationsService.saveConfigDataWizard($scope.configuration).then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 6
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaBalSheetHdService.removeAllBalanceSheetHeadings().then(function () {
                    $scope.loadBalanceSheet();
                });
            });

        }])

    .controller('importWizardController', ['$scope', '$filter', 'importDataService', '$interval',
        function ($scope, $filter, importDataService, $interval) {

            $scope.dataToimportSel = {
                id: $scope.configurationData.id,
                updateExist: false,
                hasnames: true
            };

            $scope.$on('dialogImportOpen', function (event, data) {
                $scope.clearSearch();
            });

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.dataToimportSel.previewDataFiltered.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.dataToimportSel.previewDataFiltered.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.dataToimportSel.previewDataFiltered.length;
            };

            $scope.firstToShow = "10";

            $scope.initializeUploadFile = $interval(function () {
                if ($('#upload-file').length > 0) {
                    $('#upload-file').on('change', function (e) {
                        $scope.uploadFileServer(e);
                    });
                    $interval.cancel($scope.initializeUploadFile);
                }
            }, 100);

            $scope.uploadFileServer = function (e) {
                if (e.target.files.length > 0) {
                    $scope.loadingData = { isLoading: true, message: ' Uploading file... ' };
                    setTimeout(function () {
                        $scope.$apply();
                        $scope.dataToimportSel.file = e.target.files[0].name;
                        var data = new FormData();
                        var files = e.target.files;
                        if (files.length > 0) {
                            data.append("UploadedImage", files[0]);
                        }
                        importDataService.uploadFile(data).then(function (results) {
                            $scope.dataToimportSel.fileinServer = results.data;
                            $scope.loadingData = { isLoading: false, message: ' ' };
                            $scope.dataToimportSel.previewData = null;
                            $scope.dataToimportSel.previewDataFiltered = null;
                            setTimeout(function () {
                                $scope.$apply();
                            }, 200);
                        });
                    }, 250);
                }
            };

            $scope.clearSearch = function () {
                $scope.dataToimportSel.file = null;
                $scope.dataToimportSel.fileinServer = null;
                $scope.dataToimportSel.previewData = null;
                $scope.dataToimportSel.previewDataFiltered = null;
                $scope.resultDataImp = [];
                $('#upload-file').replaceWith($('#upload-file').val("").clone(true));
            };

            $scope.previewimportData = function () {
                $scope.loadingData = { isLoading: true, message: ' Loading preview data... ' };
                setTimeout(function () {
                    $scope.$apply();
                    importDataService.previewDataImport($scope.dataToimportSel, $scope.firstToShow).then(function (results) {
                        $scope.dataToimportSel.previewData = results.data.data;
                        $scope.dataToimportSel.previewDataFiltered = $.extend([], results.data.data);
                        $scope.dataToimportSel.previewDataFiltered.splice(0, 1);
                        $scope.dataToimportSel.previewDataFiltered.splice(-1, 1);
                        $scope.pageChanged();
                        $scope.setWidthTable();
                        $scope.loadingData = { isLoading: false, message: ' ' };
                        setTimeout(function () {
                            $scope.$apply();
                        }, 200);
                    });
                }, 200);
            };

            $scope.setWidthTable = function () {
                $("#table-preview").width(Object.keys($scope.dataToimportSel.previewData[0]).length * 120);
            };

            $scope.importData = function () {
                $scope.resultDataImp = [];
                $scope.dataToimportSel.previewData = null;
                $scope.loadingData = { isLoading: true, message: " Importing data... " };
                setTimeout(function () {
                    $scope.$apply();
                    importDataService.dataImport($scope.dataToimportSel).then(function (results) {
                        $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        $scope.resultDataImp = results.data.data;
                        $scope.resultDataImp.errorFile = $.serviceBase + 'upload/' + $scope.resultDataImp.errorFile;
                        $scope.loadingData = { isLoading: false, message: null };
                        $scope.$emit('importsDone');
                        if (!results.data.error) {
                            $("#importDialog").modal("hide");
                        }
                    });
                }, 200);
            };


        }])

    .controller('balanceSheetConfigController', ["$scope", "coaBalanceSheetsService", 'coaBalSheetHdService', '$filter', 'configurationsService',
        function ($scope, coaBalanceSheetsService, coaBalSheetHdService, $filter, configurationsService) {

            $scope.balanceSheets = [];
            $scope.balanceSheetsHeadings = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.balanceSheets.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.balanceSheets.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.balanceSheets.length;
            };

            $scope.balanceSheet = {
                id: null,
                description: null,
                heading: null,
                inactive: false
            };

            $scope.loadServices = function () {
                $scope.loadBalanceSheets();
                coaBalSheetHdService.getcoaBalanceSheetHeadings(112, 100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.balanceSheetsHeadings = results.data[1];
                });
            };

            $scope.loadBalanceSheets = function () {
                coaBalanceSheetsService.getBalanceSheetsDetailed(113, 100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.balanceSheets = results.data[1];
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Balance Sheet";
                    $scope.balanceSheet = $.extend({}, $filter('filter')($scope.balanceSheets, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Balance Sheet";
                    $scope.balanceSheet = { id: null, description: null, heading: null, inactive: false };
                }
                $("#addItem").modal("show");
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 7
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.deleteItem = function () {
                coaBalanceSheetsService.deletechartOfAccountsBalanceSheets($scope.balanceSheet.id).then(function () {
                    $scope.loadBalanceSheets();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.balanceSheet.id) {
                        coaBalanceSheetsService.putCOABalSheet($scope.balanceSheet).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadBalanceSheets();
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                                $("#addItem").modal("hide");
                            }
                        });
                    } else {
                        delete $scope.balanceSheet.id;
                        coaBalanceSheetsService.postCOABalSheet($scope.balanceSheet).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadBalanceSheets();
                                $("#addItem").modal("hide");
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaBalanceSheetsService.removeAllBalanceSheet().then(function () {
                    $scope.loadBalanceSheets();
                });
            });

        }])

    .controller('trialBalanceHeadingConfigController', ['$scope', 'coaTrialBalancesHeadingsService', 'coaTrialBalancesHeadingsSectionsService', '$filter', 'configurationsService',
        function ($scope, coaTrialBalancesHeadingsService, coaTrialBalancesHeadingsSectionsService, $filter, configurationsService) {

            $scope.trialBalancesHeadings = [];
            $scope.trialBalancesHeadingsSections = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.trialBalancesHeadings.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.trialBalancesHeadings.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.trialBalancesHeadings.length;
            };

            $scope.trialBalancesHeading = {
                id: null,
                heading: null,
                section: null,
                inactive: false
            };

            $scope.loadServices = function () {
                $scope.loadTrialBalancesHeadings();
                coaTrialBalancesHeadingsSectionsService.getTrialBalancesHeadingsSections().then(function (results) {
                    $scope.trialBalancesHeadingsSections = results.data;
                });
            };

            $scope.loadTrialBalancesHeadings = function () {
                coaTrialBalancesHeadingsService.getTrialBalancesHeadings().then(function (results) {
                    $scope.trialBalancesHeadings = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Trial Balance Headings";
                    $scope.trialBalancesHeading = $.extend({}, $filter('filter')($scope.trialBalancesHeadings, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Trial Balance Headings";
                    $scope.trialBalancesHeading = { id: null, heading: null, section: null, inactive: false };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.trialBalancesHeading.id) {
                        coaTrialBalancesHeadingsService.putTrialBalanceHeadings($scope.trialBalancesHeading).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTrialBalancesHeadings();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    } else {
                        delete $scope.trialBalancesHeading.id;
                        coaTrialBalancesHeadingsService.postTrialBalanceHeadings($scope.trialBalancesHeading).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTrialBalancesHeadings();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                coaTrialBalancesHeadingsService.deleteTrialBalanceHeadings($scope.trialBalancesHeading.id).then(function () {
                    $scope.loadTrialBalancesHeadings();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 8
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaTrialBalancesHeadingsService.removeAllTrialBalanceHeadings().then(function () {
                    $scope.loadTrialBalancesHeadings();
                });
            });

        }])

    .controller('trialBalanceConfigController', ['$scope', '$filter', 'configurationsService', 'coaTrialBalancesService', 'coaTrialBalancesHeadingsService',
        function ($scope, $filter, configurationsService, coaTrialBalancesService, coaTrialBalancesHeadingsService) {

            $scope.trialBalances = [];
            $scope.trialBalancesHeadings = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.modalData = {};

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.trialBalances.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.trialBalances.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.trialBalances.length;
            };

            $scope.trialBalance = {
                id: null,
                code: null,
                description: null,
                predefined: false,
                inactive: false,
                heading: null
            };

            $scope.loadServices = function () {
                $scope.loadTrialBalances();
                coaTrialBalancesHeadingsService.getTrialBalancesHeadings().then(function (results) {
                    $scope.trialBalancesHeadings = results.data;
                });
            };

            $scope.loadTrialBalances = function () {
                coaTrialBalancesService.getTrialBalances().then(function (results) {
                    $scope.trialBalances = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Trial Balance";
                    $scope.trialBalance = $.extend({}, $filter('filter')($scope.trialBalances, { id: ind }, true)[0]);
                    $scope.trialBalance.heading = Number($scope.trialBalance.heading);
                } else {
                    $scope.modalData.titleModal = "Add Trial Balance";
                    $scope.trialBalance = { id: null, code: null, description: null, predefined: false, inactive: false, heading: null };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.trialBalance.id) {
                        coaTrialBalancesService.putTrialBalance($scope.trialBalance).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTrialBalances();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        delete $scope.trialBalance.id;
                        coaTrialBalancesService.postTrialBalance($scope.trialBalance).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTrialBalances();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                coaTrialBalancesService.deleteTrialBalance($scope.trialBalance.id).then(function () {
                    $scope.loadTrialBalances();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 9
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaTrialBalancesService.removeAllTrialBalance().then(function () {
                    $scope.loadTrialBalances();
                });
            });

        }])

    .controller("cashFlowsConfigController", ["$scope", 'coaCashFlowsService', 'coaCashFlowsGrService', '$filter', 'configurationsService',
        function ($scope, coaCashFlowsService, coaCashFlowsGrService, $filter, configurationsService) {

            $scope.cashFlows = [];
            $scope.cashFlowsGroups = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.cashFlows.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.cashFlows.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.cashFlows.length;
            };

            $scope.cashFlow = { id: null, code: null, description: null, idGroup: null, inactive: false };

            $scope.loadServices = function () {
                $scope.loadCashFlows();
                coaCashFlowsGrService.GetchartOfAccountsCashFlowGroups(100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.cashFlowsGroups = results.data;
                });
            };

            $scope.loadCashFlows = function () {
                coaCashFlowsService.GetchartOfAccountsCashFlowsDetailed(114, 100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.cashFlows = results.data[1];
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Cash Flows";
                    $scope.cashFlow = $.extend({}, $filter('filter')($scope.cashFlows, { id: ind }, true)[0]);
                    $scope.cashFlow.idGroup = Number($scope.cashFlow.idGroup)
                } else {
                    $scope.modalData.titleModal = "Add Cash Flows";
                    $scope.cashFlow = { id: null, code: null, description: null, idGroup: null, inactive: false };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.cashFlow.id) {

                        coaCashFlowsService.putCOACashFlow($scope.cashFlow).then(function (results) {
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            if (!results.data.error) {
                                $scope.loadCashFlows();
                                $("#addItem").modal("hide");
                            }
                        });
                    } else {
                        delete $scope.cashFlow.id;
                        coaCashFlowsService.postCOACashFlow($scope.cashFlow).then(function (results) {
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            if (!results.data.error) {
                                $scope.loadCashFlows();
                                $("#addItem").modal("hide");
                            }
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.deleteItem = function () {
                coaCashFlowsService.DeletechartOfAccountsCashFlow($scope.cashFlow.id).then(function () {
                    $scope.loadCashFlows();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 10
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaCashFlowsService.removeAllcashFlows().then(function () {
                    $scope.loadCashFlows();
                });
            });

        }])

    .controller("accountsGroupsConfigController", ["$scope", 'coaGroupsService', '$filter', 'configurationsService',
        function ($scope, coaGroupsService, $filter, configurationsService) {

            $scope.accountsGroups = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.accountsGroups.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.accountsGroups.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.accountsGroups.length;
            };

            $scope.accountsGroup = { id: null, code: null, description: null, inactive: false };

            $scope.loadServices = function () {
                coaGroupsService.getCoaGroups().then(function (results) {
                    $scope.accountsGroups = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Accounts Groups";
                    $scope.accountsGroup = $.extend({}, $filter('filter')($scope.accountsGroups, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Accounts Groups";
                    $scope.accountsGroup = { id: null, code: null, description: null, inactive: false };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.accountsGroup.id) {
                        coaGroupsService.putCOAGroup($scope.accountsGroup).then(function (results) {
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                            }
                        });
                    } else {
                        delete $scope.accountsGroup.id;
                        coaGroupsService.postCOAGroup($scope.accountsGroup).then(function (results) {
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                            }
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.deleteItem = function () {
                coaGroupsService.DeletechartOfAccountsGroup($scope.accountsGroup.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 11
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };


            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                coaGroupsService.removeAllGroups().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('chartOfAccountsConfigController', ['$scope', 'chartOfAccountService', 'coaTrialBalancesService', 'coaBalanceSheetsService', 'coaCashFlowsService', 'coaGroupsService', 'configurationsService', '$filter',
        function ($scope, chartOfAccountService, coaTrialBalancesService, coaBalanceSheetsService, coaCashFlowsService, coaGroupsService, configurationsService, $filter) {

            $scope.accounts = [];
            $scope.trialBalances = [];
            $scope.balanceSheets = [];
            $scope.cashFlows = [];
            $scope.accountsGroups = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.accounts.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.accounts.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.accounts.length;
            };

            $scope.account = {
                id: null,
                code: null,
                description: null,
                inactive: false,
                group: null,
                trialBalance: null,
                balanceSheet: null,
                cashFlow: null
            };

            $scope.loadServices = function () {
                $scope.loadaccounts();

                coaTrialBalancesService.getCoaTrialBalances().then(function (results) {
                    $scope.trialBalances = $filter('orderBy')(results.data, 'description');
                });

                coaBalanceSheetsService.getCoaBalanceSheets().then(function (results) {
                    $scope.balanceSheets = $filter('orderBy')(results.data, 'description');
                });

                coaCashFlowsService.getCoaCashFlows().then(function (results) {
                    $scope.cashFlows = $filter('orderBy')(results.data, 'description');
                });

                coaGroupsService.getCoaGroups().then(function (results) {
                    $scope.accountsGroups = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadaccounts = function () {
                chartOfAccountService.getGlAccountFull().then(function (results) {
                    $scope.accounts = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData = {
                        titleModal: "Edit GL Accounts"
                    }
                    $scope.account = $.extend({}, $filter('filter')($scope.accounts, { id: ind }, true)[0]);
                    $scope.account.balanceSheet = Number($scope.account.balanceSheet);
                } else {
                    $scope.modalData = {
                        titleModal: "Add GL Accounts"
                    }
                    $scope.account = {
                        id: null,
                        code: null,
                        description: null,
                        inactive: false,
                        group: null,
                        trialBalance: null,
                        balanceSheet: null,
                        cashFlow: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.account.id) {
                        chartOfAccountService.putChartOfAccount($scope.account).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadaccounts();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.account.id;
                        chartOfAccountService.postChartOfAccount($scope.account).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadaccounts();
                                $("#addItem").modal("hide");
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 12
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                chartOfAccountService.delChartOfAccount($scope.account.id).then(function (results) {
                    if (!results.data.error) {
                        $scope.loadaccounts();
                        $("#addItem").modal("hide");
                    }
                    $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                });
            };

            $scope.$on('delete-all', function (event, data) {
                chartOfAccountService.removeAllGlAccounts().then(function () {
                    $scope.loadaccounts();
                });
            });

            $scope.loadServices();



        }])

    .controller('defaultAccountsConfigController', ['$scope', 'coaDefaultsService', 'chartOfAccountService', '$filter', 'configurationsService',
        function ($scope, coaDefaultsService, chartOfAccountService, $filter, configurationsService) {

            $scope.defaultAccounts = [];
            $scope.accounts = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.defaultAccounts.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.defaultAccounts.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.defaultAccounts.length;
            };

            $scope.defaultAccount = { id: null, description: null, account: null };

            $scope.loadServices = function () {
                $scope.loaddefaultAccounts();
                chartOfAccountService.getGlAccounts(100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.accounts = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loaddefaultAccounts = function () {
                coaDefaultsService.getDefaultsAccounts(100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.defaultAccounts = results.data[1];
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData = {
                        titleModal: "Edit Defaults Accounts"
                    }
                    $scope.defaultAccount = $.extend({}, $filter('filter')($scope.defaultAccounts, { id: ind }, true)[0]);
                    $scope.defaultAccount.account = Number($scope.defaultAccount.account);
                } else {
                    $scope.modalData = {
                        titleModal: "Add Defaults Accounts"
                    }
                    $scope.defaultAccount = { id: null, description: null, account: null };
                }
                $("#addItem").modal("show");
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.defaultAccount.id) {
                        coaDefaultsService.putCoaDefaults($scope.defaultAccount).then(function (results) {
                            if (!results.data.error) {
                                $scope.loaddefaultAccounts();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 13
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('accountsByFamlilyConfigController', ['$scope', 'partsAccFamiliesService', 'chartOfAccountService', '$filter', 'configurationsService',
        function ($scope, partsAccFamiliesService, chartOfAccountService, $filter, configurationsService) {

            $scope.partsAccountsFamilies = [];
            $scope.accounts = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsAccountsFamilies.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsAccountsFamilies.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsAccountsFamilies.length;
            };

            $scope.partsAccountsFamilie = {
                id: null,
                code: null,
                description: null,
                income: null,
                taxExemptIncome: null,
                purchasesInventory: null,
                costOfGoodsSold: null,
                warranty: null,
                sample: null,
                freeGoods: null,
                inactive: false,
                unbilledPurchases: null,
                unbilledShipment: null
            };

            $scope.loadServices = function () {
                $scope.loadpartsAccountsFamilies();
                chartOfAccountService.getGlAccounts().then(function (results) {
                    $scope.accounts = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadpartsAccountsFamilies = function () {
                partsAccFamiliesService.getAccFamilyAllFully(26,100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.partsAccountsFamilies = results.data[1];
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Accounts by Part Family";
                    $scope.partsAccountsFamilie = $.extend({}, $filter('filter')($scope.partsAccountsFamilies, { id: ind }, true)[0]);
                    $scope.partsAccountsFamilie.income = Number($scope.partsAccountsFamilie.income);
                    $scope.partsAccountsFamilie.taxExemptIncome = Number($scope.partsAccountsFamilie.taxExemptIncome);
                    $scope.partsAccountsFamilie.purchasesInventory = Number($scope.partsAccountsFamilie.purchasesInventory);
                    $scope.partsAccountsFamilie.costOfGoodsSold = Number($scope.partsAccountsFamilie.costOfGoodsSold);
                    $scope.partsAccountsFamilie.warranty = Number($scope.partsAccountsFamilie.warranty);
                    $scope.partsAccountsFamilie.sample = Number($scope.partsAccountsFamilie.sample);
                    $scope.partsAccountsFamilie.freeGoods = Number($scope.partsAccountsFamilie.freeGoods);
                    $scope.partsAccountsFamilie.unbilledPurchases = Number($scope.partsAccountsFamilie.unbilledPurchases);
                    $scope.partsAccountsFamilie.unbilledShipment = Number($scope.partsAccountsFamilie.unbilledShipment);
                } else {
                    $scope.modalData.titleModal = "Add Accounts by Part Family";
                    $scope.partsAccountsFamilie = {
                        id: null,
                        code: null,
                        description: null,
                        income: null,
                        taxExemptIncome: null,
                        purchasesInventory: null,
                        costOfGoodsSold: null,
                        warranty: null,
                        sample: null,
                        freeGoods: null,
                        inactive: false,
                        unbilledPurchases: null,
                        unbilledShipment: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.partsAccountsFamilie.id) {
                        partsAccFamiliesService.putAccFamily($scope.partsAccountsFamilie).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadpartsAccountsFamilies();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.partsAccountsFamilie.id;
                        partsAccFamiliesService.postAccFamily($scope.partsAccountsFamilie).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadpartsAccountsFamilies();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 14
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                partsAccFamiliesService.delAccFamily($scope.partsAccountsFamilie.id).then(function () {
                    $scope.loadpartsAccountsFamilies();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                partsAccFamiliesService.removeAllAccFamily().then(function () {
                    $scope.loadpartsAccountsFamilies();
                });
            });

        }])

    .controller('unitsConfigController', ['$scope', 'unitsService', '$filter', 'configurationsService',
        function ($scope, unitsService, $filter, configurationsService) {

            $scope.units = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.units.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.units.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.units.length;
            };

            $scope.unit = {
                id: null,
                code: null,
                description: null,
                fraction: false,
                inactive: false
            };

            $scope.loadServices = function () {
                unitsService.gettUnitsAll().then(function (results) {
                    $scope.units = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Unit";
                    $scope.unit = $.extend({}, $filter('filter')($scope.units, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Unit";
                    $scope.unit = {
                        id: null,
                        code: null,
                        description: null,
                        fraction: false,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 15
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.unit.id) {
                        unitsService.putUnits($scope.unit).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.unit.id;
                        unitsService.postUnits($scope.unit).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                unitsService.deleteUnit($scope.unit.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                unitsService.removeAllUnits().then(function () {
                    $scope.loadServices();
                });
            });


        }])

    .controller('costingMethodsConfigController', ['$scope', 'configurationsService', 'coaTrialBalancesService', 'coaBalanceSheetsService', '$filter',
        function ($scope, configurationsService, coaTrialBalancesService, coaBalanceSheetsService, $filter) {

            $scope.configuration = {};
            $scope.checkingEmail = false;
            $scope.trialBalances = [];
            $scope.balanceSheets = [];

            $scope.yesNoValues = [
                { id: "0", name: "No" },
                { id: "1", name: "Yes" }
            ];

            $scope.cogsMethods = [
                { id: "0", name: "Periodic" },
                { id: "1", name: "Perpetual" }
            ];

            $scope.costingMethods = [
                { id: "1", name: "Average Cost" },
                { id: "2", name: "FIFO" }
            ];

            $scope.loadServices = function () {
                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        if (data.name === "PL_ACCREC" || data.name === "PL_ACCPAY") {
                            $scope.configuration[data.name] = Number(data.value);
                        } else {
                            $scope.configuration[data.name] = data.value;
                        }
                    });
                });

                coaTrialBalancesService.getTrialBalances().then(function (results) {
                    $scope.trialBalances = $filter('orderBy')(results.data, 'description');
                });

                coaBalanceSheetsService.getBalanceSheetsDetailed(113, 100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.balanceSheets = $filter('orderBy')(results.data[1], 'description');
                });
            };

            $scope.saveConfig = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.configurationData.loading = true;
                    $scope.configuration.CONFIG_STEP = "16";
                    configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                        $scope.reloadFromChild();
                    });
                }
            };

            $scope.loadServices();

        }])

    .controller('creditCardProcessorConfigController', ['$scope', 'configurationsService',
        function ($scope, configurationsService) {

            $scope.configuration = {};
            $scope.checkingEmail = false;

            $scope.loadServices = function () {
                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        $scope.configuration[data.name] = data.value;
                        if (data.name === "USE_CREDIT_CARD") {
                            $scope.configuration[data.name] = (data.value === "1");
                        }
                        if (data.name === "PASSWORD_CD") {
                            $scope.configuration['PASSWORD_CD_COPY'] = data.value;
                        }
                    });
                });
            };

            $scope.saveConfig = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    $scope.configurationData.loading = true;
                    $scope.configuration.CONFIG_STEP = "17";
                    if ($scope.configuration.USE_CREDIT_CARD) {
                        $scope.configuration.USE_CREDIT_CARD = 1;
                    }
                    configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                        $scope.reloadFromChild();
                    });
                }
            };

            $scope.loadServices();

        }])

    .controller('salesRepConfigController', ['$scope', 'salesRepsService', 'configurationsService', '$filter',
        function ($scope, salesRepsService, configurationsService, $filter) {

            $scope.salesReps = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.salesReps.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.salesReps.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.salesReps.length;
            };

            $scope.salesRep = {
                id: null,
                code: null,
                name: null,
                maxDiscount: null,
                commission: null,
                phone: null,
                cell: null,
                email: null
            };

            $scope.loadServices = function () {
                salesRepsService.getSalesReps().then(function (results) {
                    $scope.salesReps = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Sales Representative";
                    $scope.salesRep = $.extend({}, $filter('filter')($scope.salesReps, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Sales Representative";
                    $scope.salesRep = {
                        id: null,
                        code: null,
                        name: null,
                        maxDiscount: null,
                        commission: null,
                        phone: null,
                        cell: null,
                        email: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.salesRep.id) {
                        salesRepsService.putsalesReps($scope.salesRep).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.salesRep.id;
                        salesRepsService.postsalesReps($scope.salesRep).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };


            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 18
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                salesRepsService.deleteSalesRep($scope.salesRep.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                salesRepsService.removeAllSalesRep().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('shipmentsConfigController', ['$scope', 'shipmentsService', '$filter', 'configurationsService',
        function ($scope, shipmentsService, $filter, configurationsService) {

            $scope.shipments = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.shipments.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.shipments.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.shipments.length;
            };

            $scope.shipment = {
                id: null,
                code: null,
                description: null,
                carrier: null
            };

            $scope.loadServices = function () {
                shipmentsService.getShipments().then(function (results) {
                    $scope.shipments = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Shipments";
                    $scope.shipment = $.extend({}, $filter('filter')($scope.shipments, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Shipments";
                    $scope.shipment = {
                        id: null,
                        code: null,
                        description: null,
                        carrier: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.shipment.id) {
                        shipmentsService.putShipment($scope.shipment).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.shipment.id;
                        shipmentsService.postShipment($scope.shipment).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            }
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 19
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                shipmentsService.delShipment($scope.shipment.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                shipmentsService.removeAllShipment().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('taxesDetailsConfigController', ['$scope', 'taxesDetailsService', 'chartOfAccountService', '$filter', 'configurationsService',
        function ($scope, taxesDetailsService, chartOfAccountService, $filter, configurationsService) {

            $scope.taxesDetails = [];
            $scope.accounts = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.taxesDetails.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.taxesDetails.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.taxesDetails.length;
            };

            $scope.taxesDetail = {
                id: null,
                code: null,
                description: null,
                rate: null,
                account: null,
                inactive: false
            };

            $scope.loadServices = function () {
                $scope.loadtaxesDetails();
                chartOfAccountService.getGlAccounts().then(function (results) {
                    $scope.accounts = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadtaxesDetails = function () {
                taxesDetailsService.taxesDetailsDetailed().then(function (results) {
                    $scope.taxesDetails = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Tax Components";
                    $scope.taxesDetail = $.extend({}, $filter('filter')($scope.taxesDetails, { id: ind }, true)[0]);
                    $scope.taxesDetail.account = Number($scope.taxesDetail.account);
                } else {
                    $scope.modalData.titleModal = "Add Tax Components";
                    $scope.taxesDetail = {
                        id: null,
                        code: null,
                        description: null,
                        rate: null,
                        account: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.taxesDetail.id) {
                        taxesDetailsService.PuttaxesDetail($scope.taxesDetail).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadtaxesDetails();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        delete $scope.taxesDetail.id;
                        taxesDetailsService.PosttaxesDetail($scope.taxesDetail).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadtaxesDetails();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 20
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                taxesDetailsService.DeletetaxesDetail($scope.taxesDetail.id).then(function () {
                    $scope.loadtaxesDetails();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadtaxesDetails();
            });

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                taxesDetailsService.removeAlltaxesDetails().then(function () {
                    $scope.loadtaxesDetails();
                });
            });

        }])

    .controller('taxesConfigController', ['$scope', 'taxesService', '$filter', 'taxesDetailsService', '$interval', 'taxesRelationsService', 'configurationsService',
        function ($scope, taxesService, $filter, taxesDetailsService, $interval, taxesRelationsService, configurationsService) {

            $scope.taxes = [];
            $scope.taxesDetails = [];
            $scope.taxesDetailsByTax = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.taxes.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.taxes.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.taxes.length;
            };

            $scope.tax = {
                id: null,
                code: null,
                description: null,
                exempt: false,
                inactive: false,
                percent: null
            };

            $scope.loadServices = function () {
                $scope.loadtaxes();
                taxesDetailsService.GettaxesDetails($scope.tax.id).then(function (results) {
                    $scope.taxesDetails = results.data;
                });
            };

            $scope.loadtaxes = function () {
                taxesService.getTaxes().then(function (results) {
                    $scope.taxes = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Tax";
                    $scope.tax = $.extend({}, $filter('filter')($scope.taxes, { id: ind }, true)[0]);
                    $scope.loadTaxesComponents();
                } else {
                    $scope.modalData.titleModal = "Add Tax";
                    $scope.tax = {
                        id: null,
                        code: null,
                        description: null,
                        exempt: false,
                        inactive: false,
                        percent: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.loadTaxesComponents = function () {
                taxesDetailsService.getDetailsByTax($scope.tax.id).then(function (results) {
                    $scope.taxesDetailsByTax = results.data;
                    $scope.taxesDetailsByTax.push({ idRel: null, idTax: $scope.tax.id });
                    $scope.initializetaxesDetails();
                });
            };

            $scope.initializetaxesDetails = function () {
                var intervalTaxDetailCode = $interval(function () {
                    if ($("#taxesDetails [autocomplete-tax-comp='code']").length > 0) {
                        $("#taxesDetails [autocomplete-tax-comp='code']").autocomplete({
                            source: $scope.taxesDetails.map(function (data) {
                                return { id: data.id, value: data.code, description: data.description, rate: data.rate };
                            })
                        });
                        $interval.cancel(intervalTaxDetailCode);
                    }
                });

                var intervalTaxDetailDesc = $interval(function () {
                    if ($("#taxesDetails [autocomplete-tax-comp='desc']").length > 0) {
                        $("#taxesDetails [autocomplete-tax-comp='desc']").autocomplete({
                            source: $scope.taxesDetails.map(function (data) {
                                return { id: data.id, value: data.description, description: data.code, rate: data.rate };
                            })
                        });
                        $interval.cancel(intervalTaxDetailDesc);
                    }
                });
            };

            $scope.updTaxComp = function (e, type) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    this.td.id = sel.id;
                    this.td.code = type === "C" ? sel.value : sel.description;
                    this.td.description = type === "D" ? sel.value : sel.description;
                    this.td.rate = sel.rate;
                } else {
                    this.td.id = null;
                    this.td.code = null;
                    this.td.description = null;
                    this.td.rate = 0;
                }
            };

            $scope.saveTaxDetails = function (e) {
                if ($scope.validateRow(e.currentTarget)) {
                    var _this = this;
                    if (this.td.idRel) {
                        taxesRelationsService.PuttaxesRelation(this.td).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTax();
                                $scope.loadtaxes();
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        taxesRelationsService.PosttaxesRelation(this.td).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadTax();
                                $scope.loadtaxes();
                                _this.td.idRel = results.data.data.id;
                                $scope.taxesDetailsByTax.push({ idRel: null, idTax: $scope.tax.id });
                                $scope.initializetaxesDetails();
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                }
            };

            $scope.delTaxDetails = function (i) {
                if ($scope.taxesDetailsByTax[i].idRel) {
                    taxesRelationsService.DeletetaxesRelation($scope.taxesDetailsByTax[i].idRel).then(function (results) {
                        $scope.showMessageG("Information", "Deleted", 1);
                        $scope.loadTax();
                        $scope.loadtaxes();
                        $scope.taxesDetailsByTax.splice(i, 1);
                    });
                } else {
                    $scope.taxesDetailsByTax[i] = {
                        idRel: null, idTax: $scope.tax.id
                    };
                }

            };

            $scope.loadTax = function () {
                taxesService.getTax($scope.tax.id).then(function (results) {
                    $scope.tax = results.data;
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadtaxes();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 21
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.tax.id) {
                        taxesService.putTaxes($scope.tax).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadtaxes();
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.tax.id;
                        taxesService.postTaxes($scope.tax).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadtaxes();
                                $scope.tax.id = results.data.data.id;
                                $scope.taxesDetailsByTax.push({ idRel: null, idTax: $scope.tax.id });
                                $scope.initializetaxesDetails();
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                taxesService.Deletetaxe($scope.tax.id).then(function () {
                    $scope.loadtaxes();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                taxesService.removeAlltaxes().then(function () {
                    $scope.loadtaxes();
                });
            });

        }])

    .controller('paymentsTermsConfigController', ['$scope', 'paymentsTermsService', '$filter', 'configurationsService',
        function ($scope, paymentsTermsService, $filter, configurationsService) {

            $scope.paymentsTerms = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.paymentsTerms.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.paymentsTerms.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.paymentsTerms.length;
            };

            $scope.paymentsTerm = {
                id: null,
                code: null,
                terms: null,
                inactive: false,
                paymentsTermsDetail: []
            };

            $scope.loadServices = function () {
                paymentsTermsService.getPaymTermsData().then(function (results) {
                    $scope.paymentsTerms = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Payments Terms";
                    $scope.paymentsTerm = $.extend({}, $filter('filter')($scope.paymentsTerms, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Payments Terms";
                    $scope.paymentsTerm = {
                        id: null,
                        code: null,
                        terms: null,
                        inactive: false,
                        paymentsTermsDetail: [{ id: null, idPayment: null, days: 0, percent: 100 }]
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.addDetail = function () {
                $scope.paymentsTerm.paymentsTermsDetail.push({ id: null, idPayment: null, days: 0, percent: 0 });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.validateTarget($("#paymentTermsDetails"))) {
                        if ($scope.paymentsTerm.id) {
                            paymentsTermsService.putPaymTermsData($scope.paymentsTerm).then(function (results) {
                                if (!results.data.error) {
                                    $scope.loadServices();
                                    $("#addItem").modal("hide");
                                }
                                $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                            });
                        } else {
                            delete $scope.paymentsTerm.id;
                            paymentsTermsService.postPaymTermsData($scope.paymentsTerm).then(function (results) {
                                if (!results.data.error) {
                                    $scope.loadServices();
                                    $("#addItem").modal("hide");
                                }
                                $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                            });
                        }
                    }
                };
            };

            $scope.delPaymentDetails = function (i) {
                $scope.paymentsTerm.paymentsTermsDetail.splice(i, 1);
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 22
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                paymentsTermsService.DeletepaymentsTerm($scope.paymentsTerm.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                paymentsTermsService.removeAllPaymTerms().then(function (results) {
                    $scope.loadServices();
                });
            });

        }])

    .controller('customerConfigController', ['$scope', 'configurationsService', '$filter', 'customersService', 'custStatusService', 'paymentsTermsService', 'taxesService',
        function ($scope, configurationsService, $filter, customersService, custStatusService, paymentsTermsService, taxesService) {

            $scope.customers = [];
            $scope.savingData = false;
            $scope.customersStatus = [];
            $scope.paymentsTerms = [];
            $scope.taxes = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.customers.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.customers.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.customers.length;
            };

            $scope.newCustomer = function () {
                $scope.customer = {
                    id: null,
                    code: null,
                    name: null,
                    salesRep: null,
                    salesRepCommision: null,
                    zip: null,
                    address1: null,
                    address2: null,
                    city: null,
                    state: null,
                    country: null,
                    phone: null,
                    fax: null,
                    email: null,
                    webSite: null,
                    shipment: null,
                    tax: null,
                    paymentTerms: null,
                    balanceOpenOrders: 0,
                    balanceUnpaidInvoices: 0,
                    balance: 0,
                    creditLimit: 0,
                    creditAvailable: 0,
                    custGroup: null,
                    status: 1,
                    notes: null,
                    account: null,
                    hideAlias: false,
                    hidePart: false,
                    remarks: null,
                    isPlatinum: false,
                    directConnectCustomerKey: null,
                    externalAccount: null,
                    balanceCurrent: 0,
                    balance1to30: 0,
                    balance31to60: 0,
                    balance61to90: 0,
                    balanceOver90: 0
                };
            };

            $scope.loadServices = function () {
                $scope.loadCustomers();

                custStatusService.GetcustomersStatus().then(function (results) {
                    $scope.customersStatus = results.data;
                });

                paymentsTermsService.getPaymTermsData().then(function (results) {
                    $scope.paymentsTerms = $filter('orderBy')(results.data, 'terms');
                });

                taxesService.getTaxes().then(function (results) {
                    $scope.taxes = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadCustomers = function () {
                customersService.getCustomerSearch(0).then(function (results) {
                    $scope.customers = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 23
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Customer";
                    $scope.customer = $.extend({}, $filter('filter')($scope.customers, { id: ind }, true)[0]);
                    $scope.customer.status = Number($scope.customer.status);
                } else {
                    $scope.modalData.titleModal = "Add Customer";
                    $scope.newCustomer();
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.customer.id) {
                        customersService.putCustomers($scope.customer).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        delete $scope.customer.id;
                        customersService.postCustomers($scope.customer).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadCustomers();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                customersService.delCustomers($scope.customer.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                customersService.removeAllCustomers().then(function () {
                    $scope.loadCustomers();
                });
            });

        }])

    .controller('customerContactConfigController', ['$scope', 'customersContactsService', '$filter', 'configurationsService', 'customersService', '$interval',
        function ($scope, customersContactsService, $filter, configurationsService, customersService, $interval) {

            $scope.customersContacts = [];
            $scope.savingData = false;
            $scope.customers = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.customersContacts.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.customersContacts.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.customersContacts.length;
            };

            $scope.customersContact = {
                id: null,
                code: null,
                customer: null,
                firstName: null,
                lastName: null,
                phone: null,
                cell: null,
                fax: null,
                jobTitle: null,
                email: null
            };

            $scope.loadServices = function () {
                $scope.loadcustomersContacts();

                customersService.getDataCustomers().then(function (results) {
                    $scope.customers = results.data[0].description;
                    $scope.initializeIntervalCust();
                });
            };

            $scope.checkCustomer = function (e) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    $scope.customersContact.customer = sel.id;
                    $scope.customersContact.custName = sel.value;
                } else {
                    $scope.customersContact.customer = null;
                    $scope.customersContact.custName = null;
                }
            };

            $scope.initializeIntervalCust = function () {
                var intervalCust = $interval(function () {
                    if ($("[auto-complete-cust]").length > 0) {
                        $("[auto-complete-cust]").autocomplete({
                            source: $scope.customers
                        });
                        $interval.cancel(intervalCust);
                    }
                }, 200);
            };

            $scope.loadcustomersContacts = function () {
                customersContactsService.getCustomersContactsDetailed().then(function (results) {
                    $scope.customersContacts = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadcustomersContacts();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 24
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Customer Contact";
                    $scope.customersContact = $.extend({}, $filter('filter')($scope.customersContacts, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Customer Contact";
                    $scope.customersContact = {
                        id: null,
                        code: null,
                        customer: null,
                        firstName: null,
                        lastName: null,
                        phone: null,
                        cell: null,
                        fax: null,
                        jobTitle: null,
                        email: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.modalData = {};

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.customersContact.id) {
                        customersContactsService.putCustomersContacts($scope.customersContact).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcustomersContacts();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        delete $scope.customersContact.id;
                        customersContactsService.postCustomersContacts($scope.customersContact).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcustomersContacts();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                } else {
                    setTimeout(function () {
                        $scope.cleanStateErrorsClases($("#form-header"));
                    }, 2500);
                };
            };

            $scope.deleteItem = function () {
                customersContactsService.delCustomersContacts($scope.customersContact.id).then(function () {
                    $scope.loadcustomersContacts();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

            $scope.$on('delete-all', function (event, data) {
                customersContactsService.removeAllCustomersContacts().then(function () {
                    $scope.loadcustomersContacts();
                });
            });

        }])

    .controller('customerSitesConfigController', ['$scope', 'customersSitesService', 'configurationsService', '$filter', 'customersService', '$interval', 'taxesService', '$q',
        function ($scope, customersSitesService, configurationsService, $filter, customersService, $interval, taxesService, $q) {

            $scope.customersSites = [];
            $scope.savingData = false;
            $scope.customers = [];
            $scope.taxes = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.customersSites.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.customersSites.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.customersSites.length;
            };

            $scope.newCustomersSite = function () {

                var qCustomersSite = $q.defer();

                $scope.customersSite = {
                    id: null,
                    code: null,
                    customer: null,
                    description: null,
                    tax: null,
                    zip: null,
                    address1: null,
                    address2: null,
                    city: null,
                    state: null,
                    country: null,
                    remarks: null,
                    taxName: null
                };

                qCustomersSite.resolve($scope.customersSite);
                return qCustomersSite.promise;

            };

            $scope.loadServices = function () {
                $scope.loadcustomersSites();

                customersService.getDataCustomers().then(function (results) {
                    $scope.customers = results.data[0].description;
                    $scope.initializeIntervalCust();
                });

                taxesService.getTaxes().then(function (results) {
                    $scope.taxes = results.data;
                    $scope.initializeIntervalTax();
                });
            };

            $scope.initializeIntervalCust = function () {
                var intervalCust = $interval(function () {
                    if ($("[auto-complete-cust]").length > 0) {
                        $("[auto-complete-cust]").autocomplete({
                            source: $scope.customers
                        });
                        $interval.cancel(intervalCust);
                    }
                }, 200);
            };

            $scope.initializeIntervalTax = function () {
                var intervalTax = $interval(function () {
                    if ($("[auto-complete-tax]").length > 0) {
                        $("[auto-complete-tax]").autocomplete({
                            source: $scope.taxes.map(function (data) {
                                return { value: data.description, id: data.id };
                            })
                        });
                        $interval.cancel(intervalTax);
                    }
                }, 200);
            };

            $scope.checkCustomer = function (e) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    $scope.customersSite.customer = sel.id;
                    $scope.customersSite.customerName = sel.value;
                } else {
                    $scope.customersSite.customer = null;
                    $scope.customersSite.customerName = null;
                }
            };

            $scope.checkTax = function (e) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    $scope.customersSite.tax = sel.id;
                    $scope.customersSite.taxName = sel.value;
                } else {
                    $scope.customersSite.tax = null;
                    $scope.customersSite.taxName = null;
                }
            };

            $scope.loadcustomersSites = function () {
                customersSitesService.getcustomersSitesDetailed().then(function (results) {
                    $scope.customersSites = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                $scope.newCustomersSite().then(function () {
                    if (ind) {
                        $scope.modalData.titleModal = "Edit Customer Site";
                        $scope.customersSite = $.extend({}, $filter('filter')($scope.customersSites, { id: ind }, true)[0]);
                        if (!$scope.customersSite.tax) {
                            $("[auto-complete-tax]").val("");
                        }
                    } else {
                        $scope.modalData.titleModal = "Add Customer Site";
                    }
                    $("#addItem").modal("show");
                });
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.customersSite.id) {
                        customersSitesService.putCustomersSites($scope.customersSite).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcustomersSites();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    } else {
                        delete $scope.customersSite.id;
                        customersSitesService.postCustomersSites($scope.customersSite).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcustomersSites();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description + "...", results.data.errorValue);
                        });
                    }
                } else {
                    setTimeout(function () {
                        $scope.cleanStateErrorsClases($("#form-header"));
                    }, 2500);
                };
            };

            $scope.$on('delete-all', function (event, data) {
                customersSitesService.removeAllCustomersSites().then(function () {
                    $scope.loadcustomersSites();
                });
            });

            $scope.$on('importsDone', function (event, data) {
                $scope.loadcustomersSites();
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 25
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                customersSitesService.delCustomersSites($scope.customersSite.id).then(function () {
                    $scope.loadcustomersSites();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();


        }])

    .controller('vendorsConfigController', ['$scope', 'vendorsService', 'configurationsService', '$filter', 'paymentsTermsService', 'currenciesService', '$interval',
        function ($scope, vendorsService, configurationsService, $filter, paymentsTermsService, currenciesService, $interval) {

            $scope.vendors = [];
            $scope.savingData = false;
            $scope.paymentsTerms = [];
            $scope.currencies = [];
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.vendors.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.vendors.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.vendors.length;
            };

            $scope.newVendor = function () {
                $scope.vendor = {
                    id: null,
                    vendor: null,
                    zip: null,
                    address1: null,
                    address2: null,
                    city: null,
                    state: null,
                    country: null,
                    phone: null,
                    fax: null,
                    email: null,
                    webSite: null,
                    taxCode: null,
                    status: null,
                    balance: 0,
                    paymentTerms: null,
                    account: null,
                    hideAlias: null,
                    hidePart: null,
                    remarks: null,
                    remarksInternal: null,
                    inactive: false,
                    is1099: false,
                    vendGroup: null,
                    externalAccount: null,
                    defaultCurrency: null
                };
            };

            $scope.loadServices = function () {
                $scope.loadVendors();
                $scope.newVendor();

                configurationsService.Getconfig().then(function (results) {
                    results.data.forEach(function (data) {
                        if (data.name === "SYSTEM_CURRENCY") {
                            $scope.configuration[data.name] = Number(data.value);
                            if (!$scope.vendor.id) {
                                $scope.vendor.defaultCurrency = Number(data.value);
                            }
                        } else {
                            $scope.configuration[data.name] = data.value;
                        }
                    });
                });

                paymentsTermsService.getPaymTermsData().then(function (results) {
                    $scope.paymentsTerms = $filter('orderBy')(results.data, 'terms');
                });

                currenciesService.getCurrencies().then(function (results) {
                    $scope.currencies = results.data;
                });
            };

            $scope.loadVendors = function () {
                vendorsService.getVendorSearch(100, 1, "", "", false, { "filterData": {} }).then(function (results) {
                    $scope.vendors = results.data[0];
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                $scope.newVendor();
                if (ind) {
                    $scope.modalData.titleModal = "Edit Vendor";
                    $scope.vendor = $.extend({}, $filter('filter')($scope.vendors, { id: ind }, true)[0]);
                    $scope.vendor.defaultCurrency = Number($scope.vendor.defaultCurrency);
                } else {
                    $scope.modalData.titleModal = "Add Vendor";
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.vendor.id) {
                        vendorsService.putVendor($scope.vendor).success(function (results) {
                            $scope.showMessageG("Information", "Saved", 1);
                            $scope.loadVendors();
                            $("#addItem").modal("hide");
                        }).error(function (data) {
                            $scope.showMessageG("Information", data.Message + "...", 2);
                        });
                    } else {
                        delete $scope.vendor.id;
                        vendorsService.postVendor($scope.vendor).success(function (results) {
                            $scope.showMessageG("Information", "Saved", 1);
                            $scope.loadVendors();
                            $("#addItem").modal("hide");
                        }).error(function (data) {
                            $scope.showMessageG("Information", data.Message + "...", 2);
                        });
                    }
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 26
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadVendors();
            });

            $scope.$on('delete-all', function (event, data) {
                vendorsService.removeAllVendors().then(function () {
                    $scope.loadVendors();
                });
            });

            $scope.deleteItem = function () {
                vendorsService.delVendors($scope.vendor.id).then(function () {
                    $scope.loadVendors();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

        }])

    .controller('vendorsContactConfigController', ['$scope', 'vendorsContactsService', 'configurationsService', 'vendorsService', '$interval', '$filter',
        function ($scope, vendorsContactsService, configurationsService, vendorsService, $interval, $filter) {

            $scope.vendorsContacts = [];
            $scope.savingData = false;
            $scope.vendors = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.vendorsContacts.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.vendorsContacts.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.vendorsContacts.length;
            };

            $scope.vendorsContact = {
                id: null,
                code: null,
                vendor: null,
                firstName: null,
                lastName: null,
                phone: null,
                cell: null,
                fax: null,
                jobTitle: null,
                email: null,
                inactive: false
            };

            $scope.loadServices = function () {
                $scope.loadvendorsContacts();

                vendorsService.getBasicDataVendorsD().then(function (results) {
                    $scope.vendors = results.data;
                    $scope.initializeIntervalVendors();
                });
            };

            $scope.checkVendor = function (e) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    $scope.vendorsContact.vendor = sel.id;
                    $scope.vendorsContact.vendorName = sel.value;
                } else {
                    $scope.vendorsContact.vendor = null;
                    $scope.vendorsContact.vendorName = null;
                }
            };

            $scope.modalData = {};

            $scope.initializeIntervalVendors = function () {
                var intervalVendor = $interval(function () {
                    if ($("[auto-complete-vendors]").length > 0) {
                        $("[auto-complete-vendors]").autocomplete({
                            source: $scope.vendors
                        });
                        $interval.cancel(intervalVendor);
                    }
                }, 200);
            };

            $scope.loadvendorsContacts = function () {
                vendorsContactsService.getVendorsContactsDetailed().then(function (results) {
                    $scope.vendorsContacts = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Vendor Contact";
                    $scope.vendorsContact = $.extend({}, $filter('filter')($scope.vendorsContacts, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Vendor Contact";
                    $scope.vendorsContact = {
                        id: null,
                        code: null,
                        vendor: null,
                        firstName: null,
                        lastName: null,
                        phone: null,
                        cell: null,
                        fax: null,
                        jobTitle: null,
                        email: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadvendorsContacts();
            });

            $scope.$on('delete-all', function (event, data) {
                vendorsContactsService.removeAllVendorsContacts().then(function () {
                    $scope.loadvendorsContacts();
                });
            });

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.vendorsContact.id) {
                        vendorsContactsService.putVendorsContacts($scope.vendorsContact).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadvendorsContacts();
                                $("#addItem").modal("hide");
                            }
                        });
                    } else {
                        delete $scope.vendorsContact.id;
                        vendorsContactsService.postVendorsContacts($scope.vendorsContact).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadvendorsContacts();
                                $("#addItem").modal("hide");
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                } else {
                    setTimeout(function () {
                        $scope.cleanStateErrorsClases($("#form-header"));
                    }, 2500);
                };
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 27
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                vendorsContactsService.DeletevendorsContacts($scope.vendorsContact.id).then(function () {
                    $scope.loadvendorsContacts();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

        }])

    .controller('warehousesConfigController', ['$scope', 'warehousesService', '$filter', 'configurationsService',
        function ($scope, warehousesService, $filter, configurationsService) {

            $scope.warehouses = [];
            $scope.savingData = false;

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.warehouses.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.warehouses.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.warehouses.length;
            };

            $scope.warehouse = {
                id: null,
                code: null,
                description: null,
                inventoryValued: false,
                transactionCosting: false,
                ignoredByPlaning: false,
                salesWarehouse: true,
                inactive: false,
                balance: 0,
                idBranch: null,
                address1: null,
                address2: null,
                city: null,
                state: null,
                zip: null,
                country: null
            };

            $scope.loadServices = function () {
                warehousesService.getWarehouses().then(function (results) {
                    $scope.warehouses = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Warehouse";
                    $scope.warehouse = $.extend({}, $filter('filter')($scope.warehouses, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Warehouse";
                    $scope.warehouse = {
                        id: null,
                        code: null,
                        description: null,
                        inventoryValued: false,
                        transactionCosting: false,
                        ignoredByPlaning: false,
                        salesWarehouse: true,
                        inactive: false,
                        balance: 0,
                        idBranch: null,
                        address1: null,
                        address2: null,
                        city: null,
                        state: null,
                        zip: null,
                        country: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.warehouse.id) {
                        warehousesService.putWarehouses($scope.warehouse).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.warehouse.id;
                        warehousesService.postWarehouses($scope.warehouse).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            }
                        });
                    }
                };
            };

            $scope.loadServices();

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 28
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                warehousesService.Deletewarehouse($scope.warehouse.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                warehousesService.removeAllWarehouses().then(function () {
                    $scope.loadServices();
                });
            });

        }])

    .controller('warehousesBinsConfigController', ['$scope', 'warehousesBinsService', '$filter', 'configurationsService', 'warehousesService', '$interval',
        function ($scope, warehousesBinsService, $filter, configurationsService, warehousesService, $interval) {

            $scope.warehousesBins = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.warehousesBins.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.warehousesBins.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.warehousesBins.length;
            };

            $scope.warehousesBin = {
                id: null,
                warehouse: null,
                aisle: null,
                row: null,
                level: null,
                slot: null,
                balance: 0
            };

            $scope.loadServices = function () {
                $scope.loadwarehousesBins();

                warehousesService.getWarehouses().then(function (results) {
                    $scope.warehouses = results.data;
                    $scope.initializeIntervalWarehouses();
                });
            };

            $scope.checkWarehouse = function (e) {
                var sel = $scope.valAutoComplete(e.currentTarget, sel);
                if (sel) {
                    $scope.warehousesBin.warehouse = sel.id;
                    $scope.warehousesBin.description = sel.value;
                } else {
                    $scope.warehousesBin.warehouse = null;
                    $scope.warehousesBin.description = null;
                }
            };

            $scope.initializeIntervalWarehouses = function () {
                var intervalWarehouses = $interval(function () {
                    if ($("[auto-complete-warehouse]").length > 0) {
                        $("[auto-complete-warehouse]").autocomplete({
                            source: $scope.warehouses.map(function (data) {
                                return { value: data.description, id: data.code };
                            })
                        });
                        $interval.cancel(intervalWarehouses);
                    }
                }, 200);
            };

            $scope.loadwarehousesBins = function () {
                warehousesBinsService.getWarehouseBinDataDetailed().then(function (results) {
                    $scope.warehousesBins = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.warehousesBin.id) {
                        warehousesBinsService.putBinByWare($scope.warehousesBin).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            } else {
                                $scope.loadwarehousesBins();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.warehousesBin.id;
                        warehousesBinsService.postBinByWare($scope.warehousesBin).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadwarehousesBins();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                            }
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadwarehousesBins();
            });

            $scope.$on('delete-all', function (event, data) {
                warehousesBinsService.removeAllWarehouseBin().then(function () {
                    $scope.loadwarehousesBins();
                });
            });

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Warehouse";
                    $scope.warehousesBin = $.extend({}, $filter('filter')($scope.warehousesBins, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Warehouse";
                    $scope.warehousesBin = {
                        id: null,
                        warehouse: null,
                        aisle: null,
                        row: null,
                        level: null,
                        slot: null,
                        balance: 0
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 29
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.deleteItem = function () {
                warehousesBinsService.DeletewarehousesBin($scope.warehousesBin.id).then(function () {
                    $scope.loadwarehousesBins();
                    $("#addItem").modal("hide");
                });
            };

            $scope.loadServices();

        }])

    .controller('partsBrandsConfigController', ['$scope', 'partsBrandsService', 'configurationsService', '$filter',
        function ($scope, partsBrandsService, configurationsService, $filter) {

            $scope.partsBrands = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsBrands.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsBrands.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsBrands.length;
            };

            $scope.partsBrand = {
                id: null,
                code: null,
                description: null,
                image: null,
                inactive: false
            };

            $scope.loadServices = function () {
                partsBrandsService.GetpartsBrands().then(function (results) {
                    $scope.partsBrands = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Part Brand";
                    $scope.partsBrand = $.extend({}, $filter('filter')($scope.partsBrands, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Part Brand";
                    $scope.partsBrand = {
                        id: null,
                        code: null,
                        description: null,
                        image: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.partsBrand.id) {
                        partsBrandsService.putPartsBrands($scope.partsBrand).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.partsBrand.id;
                        partsBrandsService.postPartsBrands($scope.partsBrand).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                partsBrandsService.DeletepartsBrand($scope.partsBrand.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partsBrandsService.removeAllpartsBrands().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 30
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('partsClasssConfigController', ['$scope', 'partsClassesService', 'configurationsService', '$filter',
        function ($scope, partsClassesService, configurationsService, $filter) {

            $scope.partsClasses = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsClasses.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsClasses.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsClasses.length;
            };

            $scope.partsClass = {
                id: null,
                code: null,
                description: null,
                inactive: false
            };

            $scope.loadServices = function () {
                partsClassesService.GetpartsClasses().then(function (results) {
                    $scope.partsClasses = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Part Brand";
                    $scope.partsClass = $.extend({}, $filter('filter')($scope.partsClasses, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Part Brand";
                    $scope.partsClass = {
                        id: null,
                        code: null,
                        description: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.partsClass.id) {
                        partsClassesService.putPartsClasses($scope.partsClass).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.partsClass.id;
                        partsClassesService.postPartsClasses($scope.partsClass).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                partsClassesService.DeletepartsClass($scope.partsClass.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partsClassesService.removeAllpartsClasses().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 31
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('partsFamiliesConfigController', ['$scope', 'partsFamiliesService', 'configurationsService', '$filter',
        function ($scope, partsFamiliesService, configurationsService, $filter) {

            $scope.partsFamilies = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsFamilies.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsFamilies.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsFamilies.length;
            };

            $scope.partsFamily = {
                id: null,
                code: null,
                description: null,
                inactive: false
            };

            $scope.loadServices = function () {
                partsFamiliesService.getPartsFamiliesDefault().then(function (results) {
                    $scope.partsFamilies = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Part Family";
                    $scope.partsFamily = $.extend({}, $filter('filter')($scope.partsFamilies, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Part Family";
                    $scope.partsFamily = {
                        id: null,
                        code: null,
                        description: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.partsFamily.id) {
                        partsFamiliesService.putPartsFamilies($scope.partsFamily).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.partsFamily.id;
                        partsFamiliesService.postPartsFamilies($scope.partsFamily).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                partsFamiliesService.DeletepartsClass($scope.partsFamily.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partsFamiliesService.removeAllpartsFamilies().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 32
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('partsGroupsConfigController', ['$scope', 'partsGroupsService', 'configurationsService', '$filter',
        function ($scope, partsGroupsService, configurationsService, $filter) {

            $scope.partsGroups = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsGroups.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsGroups.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsGroups.length;
            };

            $scope.partsGroup = {
                id: null,
                code: null,
                description: null,
                inactive: false
            };

            $scope.loadServices = function () {
                partsGroupsService.GetpartsGroups().then(function (results) {
                    $scope.partsGroups = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData.titleModal = "Edit Part Group";
                    $scope.partsGroup = $.extend({}, $filter('filter')($scope.partsGroups, { id: ind }, true)[0]);
                } else {
                    $scope.modalData.titleModal = "Add Part Group";
                    $scope.partsGroup = {
                        id: null,
                        code: null,
                        description: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.partsGroup.id) {
                        partsGroupsService.putPartsGroups($scope.partsGroup).then(function (results) {
                            if (results.data.error) {
                                $scope.showMessageG("Information", results.data.description, 2);
                            } else {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            }
                        });
                    } else {
                        delete $scope.partsGroup.id;
                        partsGroupsService.postPartsGroups($scope.partsGroup).then(function (results) {
                            if (results.data.data.id) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                                $scope.showMessageG("Information", "Saved", 1);
                            } else {
                                $scope.showMessageG("Information", results.data.description, 2);
                            }
                        });
                    }
                };
            };

            $scope.deleteItem = function () {
                partsGroupsService.DeletepartsGroup($scope.partsGroup.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partsGroupsService.removeAllpartsGroups().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 33
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('partsTypesConfigController', ['$scope', 'partsTypesService', 'configurationsService', '$filter',
        function ($scope, partsTypesService, configurationsService, $filter) {

            $scope.partsTypes = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsTypes.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsTypes.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsTypes.length;
            };

            $scope.partsType = {
                id: null,
                code: null,
                description: null
            };

            $scope.loadServices = function () {
                partsTypesService.getPartsConfig().then(function (results) {
                    $scope.partsTypes = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                $scope.modalData.titleModal = "Edit Part Catalog";
                $scope.partsType = $.extend({}, $filter('filter')($scope.partsTypes, { id: ind }, true)[0]);
                $("#addItem").modal("show");
            };

            $scope.deleteItem = function () {
                partsTypesService.delPartsTypes($scope.partsType.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partsTypesService.removeAllpartsTypes().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 34
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('customerPartsAliasConfigController', ['$scope', 'partAliasService', 'configurationsService', '$filter',
        function ($scope, partAliasService, configurationsService, $filter) {

            $scope.partsAlias = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsAlias.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsAlias.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsAlias.length;
            };

            $scope.partsAlia = {
                id: null,
                partId: null,
                customer: null,
                alias: null,
                description: null,
                partDescription: null,
                price: null,
                createdOn: null,
                startDate: null,
                endDate: null,
                inactive: null,
                unitconvertion: null,
                minimum: null,
                increment: null,
                leadTime: null,
                myUnit: null
            };

            $scope.loadServices = function () {
                partAliasService.getPartsAliasDetailed(1).then(function (results) {
                    $scope.partsAlias = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                $scope.modalData.titleModal = "Edit Part Catalog";
                $scope.partsAlia = $.extend({}, $filter('filter')($scope.partsAlias, { id: ind }, true)[0]);
                $("#addItem").modal("show");
            };

            $scope.deleteItem = function () {
                partAliasService.DeletepartsAlia($scope.partsAlia.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partAliasService.removeAllpartsAlias(1).then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 35
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('vendorPartsAliasConfigController', ['$scope', 'partAliasService', 'configurationsService', '$filter',
        function ($scope, partAliasService, configurationsService, $filter) {

            $scope.partsAlias = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.partsAlias.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.partsAlias.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.partsAlias.length;
            };

            $scope.partsAlia = {
                id: null,
                partId: null,
                customer: null,
                alias: null,
                description: null,
                partDescription: null,
                price: null,
                createdOn: null,
                startDate: null,
                endDate: null,
                inactive: null,
                unitconvertion: null,
                minimum: 0,
                increment: 0,
                leadTime: null,
                myUnit: null
            };

            $scope.loadServices = function () {
                partAliasService.getPartsAliasDetailed(0).then(function (results) {
                    $scope.partsAlias = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                $scope.modalData.titleModal = "Edit Part Catalog";
                $scope.partsAlia = $.extend({}, $filter('filter')($scope.partsAlias, { id: ind }, true)[0]);
                $("#addItem").modal("show");
            };

            $scope.deleteItem = function () {
                partAliasService.DeletepartsAlia($scope.partsAlia.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };


            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                partAliasService.removeAllpartsAlias(0).then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 36
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('banksConfigController', ['$scope', 'configurationsService', 'banksService', 'chartOfAccountService', '$filter',
        function ($scope, configurationsService, banksService, chartOfAccountService, $filter) {

            $scope.banks = [];
            $scope.savingData = false;
            $scope.configuration = {};
            $scope.accounts = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.banks.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.banks.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.banks.length;
            };

            $scope.bank = {
                id: null,
                code: null,
                description: null,
                bankCode: null,
                bankDescription: null,
                account: null,
                inactive: null
            };

            $scope.loadServices = function () {
                $scope.loadbanks();
                chartOfAccountService.getGlAccounts().then(function (results) {
                    $scope.accounts = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadbanks = function () {
                banksService.GetbanksSearch().then(function (results) {
                    $scope.banks = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData = {
                        titleModal: "Edit Bank"
                    }
                    $scope.bank = $.extend({}, $filter('filter')($scope.banks, { id: ind }, true)[0]);
                } else {
                    $scope.modalData = {
                        titleModal: "Add Bank"
                    }
                    $scope.bank = {
                        id: null,
                        code: null,
                        description: null,
                        bankCode: null,
                        bankDescription: null,
                        account: null,
                        inactive: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.bank.id) {
                        banksService.putBanks($scope.bank).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadbanks();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    } else {
                        delete $scope.bank.id;
                        banksService.postBanks($scope.bank).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadbanks();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                banksService.removeAllbanks(0).then(function () {
                    $scope.loadbanks();
                });
            });

            $scope.deleteItem = function () {
                banksService.Deletebank($scope.bank.id).then(function () {
                    $scope.loadbanks();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 37
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('cashierConfigController', ['$scope', 'configurationsService', 'cashiersService', 'chartOfAccountService', '$filter',
        function ($scope, configurationsService, cashiersService, chartOfAccountService, $filter) {

            $scope.cashiers = [];
            $scope.savingData = false;
            $scope.configuration = {};
            $scope.accounts = [];

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.cashiers.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.cashiers.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.cashiers.length;
            };

            $scope.cashier = {
                id: null,
                code: null,
                description: null,
                inactive: false,
                account: null,
                closingdate: null,
                cashierCode: null,
                userId: null
            };

            $scope.loadServices = function () {
                $scope.loadcashiers();
                chartOfAccountService.getGlAccounts().then(function (results) {
                    $scope.accounts = $filter('orderBy')(results.data, 'description');
                });
            };

            $scope.loadcashiers = function () {
                cashiersService.getCashiersData().then(function (results) {
                    $scope.cashiers = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData = {
                        titleModal: "Edit Cashier"
                    }
                    $scope.cashier = $.extend({}, $filter('filter')($scope.cashiers, { id: ind }, true)[0]);
                    $scope.cashier.account = Number($scope.cashier.account);
                } else {
                    $scope.modalData = {
                        titleModal: "Add Cashier"
                    }
                    $scope.cashier = {
                        id: null,
                        code: null,
                        description: null,
                        inactive: false,
                        account: null,
                        closingdate: null,
                        cashierCode: null,
                        userId: null
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.cashier.id) {
                        cashiersService.putCashiers($scope.cashier).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcashiers();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    } else {
                        delete $scope.cashier.id;
                        cashiersService.postCashiers($scope.cashier).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadcashiers();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                cashiersService.removeAllcashiers(0).then(function () {
                    $scope.loadcashiers();
                });
            });

            $scope.deleteItem = function () {
                cashiersService.DeleteCashier($scope.cashier.id).then(function () {
                    $scope.loadcashiers();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 38
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('branchesConfigController', ['$scope', 'configurationsService', 'branchesService', '$filter',
        function ($scope, configurationsService, branchesService, $filter) {

            $scope.branches = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.branches.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.branches.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.branches.length;
            };

            $scope.branch = {
                id: null,
                code: null,
                description: null,
                prefix: null,
                inactive: false
            };

            $scope.loadServices = function () {
                branchesService.getBranchesAll().then(function (results) {
                    $scope.branches = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.modalData = {};

            $scope.addItem = function (ind) {
                if (ind) {
                    $scope.modalData = {
                        titleModal: "Edit Cashier"
                    }
                    $scope.branch = $.extend({}, $filter('filter')($scope.branches, { id: ind }, true)[0]);
                } else {
                    $scope.modalData = {
                        titleModal: "Add Cashier"
                    }
                    $scope.branch = {
                        id: null,
                        code: null,
                        description: null,
                        prefix: null,
                        inactive: false
                    };
                }
                $("#addItem").modal("show");
            };

            $scope.saveItem = function () {
                if ($scope.validateTarget($("#form-header"))) {
                    if ($scope.branch.id) {
                        branchesService.putBranches($scope.branch).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    } else {
                        delete $scope.branch.id;
                        branchesService.postBranches($scope.branch).then(function (results) {
                            if (!results.data.error) {
                                $scope.loadServices();
                                $("#addItem").modal("hide");
                            }
                            $scope.showMessageG("Information", results.data.description, results.data.errorValue);
                        });
                    }
                };
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                branchesService.removeAllbranches(0).then(function () {
                    $scope.loadServices();
                });
            });

            $scope.deleteItem = function () {
                branchesService.deleteBranches($scope.branch.id).then(function () {
                    $scope.loadServices();
                    $("#addItem").modal("hide");
                });
            };

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 39
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };

            $scope.loadServices();

        }])

    .controller('openBalanceCustomersConfigController', ['$scope', 'journalService', 'configurationsService',
        function ($scope, journalService, configurationsService) {

            $scope.journals = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.journals.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.journals.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.journals.length;
            };

            $scope.loadServices = function () {
                journalService.getJournalByTransactionConfig("OBC").then(function (results) {
                    $scope.journals = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                journalService.removeAllOpenBalance("OBC").then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 40
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };


            $scope.loadServices();

        }])

    .controller('openBalanceVendorsConfigController', ['$scope', 'journalService', 'configurationsService',
        function ($scope, journalService, configurationsService) {

            $scope.journals = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.journals.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.journals.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.journals.length;
            };

            $scope.loadServices = function () {
                journalService.getJournalByTransactionConfig("OBV").then(function (results) {
                    $scope.journals = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                journalService.removeAllOpenBalance("OBV").then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 41
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };


            $scope.loadServices();

        }])

    .controller('openBalanceGlAccountsConfigController', ['$scope', 'journalService', 'configurationsService',
        function ($scope, journalService, configurationsService) {

            $scope.journals = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.journals.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.journals.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.journals.length;
            };

            $scope.loadServices = function () {
                journalService.getJournalGlTransactionConfig("OBA").then(function (results) {
                    $scope.journals = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                journalService.removeAllOpenBalance("OBA").then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 42
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };


            $scope.loadServices();

        }])

    .controller('inventoryCountConfigController', ['$scope', 'documentService', 'configurationsService',
        function ($scope, documentService, configurationsService) {

            $scope.documents = [];
            $scope.savingData = false;
            $scope.configuration = {};

            $scope.searchPaginator = {
                totalItems: 0,
                currentPage: 1,
                maxSize: 10
            };

            $scope.pageChanged = function () {
                $scope.searchPaginator.totalItems = $scope.documents.length;
                $scope.searchPaginator.iniPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) - $scope.searchPaginator.maxSize + 1;
                $scope.searchPaginator.endPage = ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) < $scope.documents.length ? ($scope.searchPaginator.currentPage * $scope.searchPaginator.maxSize) : $scope.documents.length;
            };

            $scope.loadServices = function () {
                documentService.getInventoryCountConfig().then(function (results) {
                    $scope.documents = results.data;
                    $scope.pageChanged();
                });
            };

            $scope.$on('importsDone', function (event, data) {
                $scope.loadServices();
            });

            $scope.$on('delete-all', function (event, data) {
                documentService.deleteAllInventoryCountConfig().then(function () {
                    $scope.loadServices();
                });
            });

            $scope.saveConfig = function () {
                $scope.configuration = {
                    CONFIG_STEP: 43
                };
                configurationsService.saveConfigDataWizard($scope.configuration).then(function (results) {
                    $scope.reloadFromChild();
                });
            };


            $scope.loadServices();

        }])

    .controller('finalizeConfigController', ['$scope', 'configurationStepsService', '$filter',
        function ($scope, configurationStepsService, $filter) {

            $scope.configurationSteps = [];

            $scope.loadServices = function () {
                configurationStepsService.GetconfigurationSteps().then(function (results) {
                    results.data.splice(-1, 1);
                    $scope.configurationSteps = results.data;
                });
            };

            $scope.checkConfig = function () {
                configurationStepsService.checkConfig().then(function (results) {
                    if (results.data.error) {
                        results.data.data.splice(-1, 1);
                        $scope.configurationSteps = results.data.data;
                    } else {
                        window.location.reload();
                    }
                });
            }

            $scope.modalData = {};

            $scope.showDetails = function (ind) {
                $scope.configurationStep = $.extend({}, $filter('filter')($scope.configurationSteps, { step: ind }, true)[0]);
                $scope.modalData = { titleModal: $scope.configurationStep.titlePage };
                if ($scope.configurationStep.validationDetail) {
                    $scope.validationDetailSplit = $scope.configurationStep.validationDetail.split(",");
                } else {
                    $scope.validationDetailSplit = [];
                }

                $("#showDetails").modal("show");
            };

            $scope.loadServices();

        }])

;