/** * * appCtrl * */ angular .module('homer') .controller('appCtrl', appCtrl); angular .module('homer').service('global', function ($firebaseObject, $firebaseArray, $http, $window) { this.auth = 'N'; this.modules = null; this.roles = null; this.menues = {}; this.para = {}; this.profile = {}; // this.menues['common.login']={module: "default", url: "login", title: "Login", class: "pe pe-7s-note text-success", sidemenu: "N"}; // this.menues.landing={module: "default", url: "landing_page", title: "Landing", class: "pe pe-7s-note text-success", sidemenu: "N"}; // this.menues.dashboard={module: "default", url: "dashboard", title: "Dashboard", class: "pe pe-7s-note text-success", sidemenu: "N"}; this.download = function (dataURI, filename) { var isChrome = !!window.chrome && !!window.chrome.webstore; if (isChrome) { var link = angular.element(document.createElement('a')); link.attr('href', dataURI); link.attr('download', filename); $timeout(function () { link[0].click(); }); } else { var binary = atob(dataURI.split(',')[1]); var array = []; for (var i = 0; i < binary.length; i++) { array.push(binary.charCodeAt(i)); } var blob = new Blob([new Uint8Array(array)], { type: 'application/file' }); if (window.navigator.msSaveOrOpenBlob) { window.navigator.msSaveOrOpenBlob(blob, filename); } else { //everything else var url = window.URL || window.webkitURL; var fileUrl = url.createObjectURL(blob); $window.open(fileUrl, filename); url.revokeObjectURL(fileUrl); } } } this.date_format = function (d) { var today = new Date(d); var dd = today.getDate(); var mm = today.getMonth() + 1; //January is 0! var yyyy = today.getFullYear(); if (dd < 10) { dd = '0' + dd; } if (mm < 10) { mm = '0' + mm; } return (yyyy + '-' + mm + '-' + dd); } this.set_para = function (name, val) { this.para[name] = val; } this.get_para = function (name) { return this.para[name]; } this.set_profile = function (profile) { console.log('Set Profile', profile); this.profile = profile; } this.set_auth = function (auth) { this.auth = auth; } this.set_modules = function (data) { this.modules = data; } this.set_roles = function (data) { this.roles = data; } this.set_menues = function (data) { this.menues = data; this.menues.dashboard = { module: "default", url: "dashboard", title: "Dashboard", class: "pe pe-7s-note text-success", sidemenu: "N" }; } this.addNoti = function (msg, link) { var ref = firebase.database().ref().child(appid + "/" + this.profile.USER.USERNAME + "/new_inbox"); var new_inbox = $firebaseArray(ref); new_inbox.$loaded( function (data) { new_inbox.$add({ text: msg, link: link }); }, function (err) { } ); } this.addAlert = function (msg, type) { var ref = firebase.database().ref().child(appid + "/" + this.profile.USER.USERNAME + "/alert"); var myalert = $firebaseArray(ref); myalert.$loaded( function (data) { myalert.$add({ text: msg, type: type }); }, function (err) { } ); } this.post = function (parameter) { var url = 'core/service.php'; return $http.post(url, parameter); } this.parseDate = function (d) { if (d == '0000-00-00') { return null; } else if (d == '1970-01-01') { return null; } else if (d == '') { return null; } else if (d == undefined) { return null; } else if (d == null) { return null; } else { return new Date(d); } } this.master = function () { return { grn_type: ['Normal'], grn_source: ['Customer_Order'], order_type: ['Normal'], gate_entry_no: ["Gate_01", "Gate_02", "Gate_03"], supplier: [{ code: "Test", name: "Test Company" }], grn_status: ["DRAFT", "WAIT FOR RECEIVE", "RECEIVED", "WAIT FOR PUTAWAY", "PUTAWAY", "WAIT FOR INSPECT", "REJECTED", "RETURN"], }; }; }); angular .module('homer').directive('compile', function ($compile, $timeout) { return { restrict: 'A', link: function (scope, elem, attrs) { $timeout(function () { $compile(elem.contents())(scope); console.log("test"); }); } }; }); function appCtrl(global, $state, $http, $scope, $timeout, $rootScope, sweetAlert, $firebaseAuth, $firebaseObject, $firebaseArray, notify, $window) { $scope.app = { title: apptitle, id: appid, footer: appFooter, footer_left: appFooterLeft, footer_right: appFooterRight }; $rootScope.printCharacteristic = null; $scope.connect_printer = function () { console.log("test"); if ($rootScope.printCharacteristic == null) { navigator.bluetooth.requestDevice({ filters: [{ services: ['000018f0-0000-1000-8000-00805f9b34fb'] }] }) .then(device => { console.log('> Found ' + device.name); console.log('Connecting to GATT Server...'); return device.gatt.connect(); }) .then(server => server.getPrimaryService("000018f0-0000-1000-8000-00805f9b34fb")) .then(service => service.getCharacteristic("00002af1-0000-1000-8000-00805f9b34fb")) .then(characteristic => { $scope.$apply(function () { $scope.printerActived = 'Y'; global.profile.printerActived = $scope.printerActived; // Cache the characteristic $rootScope.printCharacteristic = characteristic; //sendPrinterData(); $rootScope.sendPrinterText("Connected Printer (" + version + ") .. Success"); global.set_para('printer', characteristic); }); }) .catch(function (error) { console.log(error); $rootScope.printCharacteristic = null; }); } else { $scope.initPrinterText(); $rootScope.sendPrinterText("\nConnected Printer (" + version + ") .. Test Print\n"); $rootScope.sendPrinterText("\nConnected Printer (" + version + ") .. ทดสอบภาษาไทย\n"); } } $scope.initPrinterText = function () { console.log("Init Printer "); } $rootScope.sendPrinterText2 = function (textstr) { // let encoder = new TextEncoder("utf-8"); // let text = encoder.encode('\x1B\x74\x60\u0E01'+"กNo 60 ภาษาไทย "+textstr+ '\u000A\u000D'+'\u000A\u000D'+'\u000A\u000D'); var str = '\x1B\x74\xFF' + textstr + '\u000A\u000D'; //var str = '\x1B\x74\x60' + textstr + '\u000A\u000D'; var bytes = new Uint8Array(textstr.length + 5); // char codes for (var i = 0; i < str.length; ++i) { var code = str.charCodeAt(i); if (code > 3424) { code -= 3424; } bytes[i] = code; } //console.log('bytes', bytes); return $rootScope.printCharacteristic.writeValue(bytes).then(() => { console.log('Write done.'); }); } $rootScope.sendPrinterText = function (textstr) { console.log(textstr.length); var str = '\x1B\x74\xFF' + textstr + '\u000A\u000D'; var bytes = new Uint8Array(textstr.length + 5); // char codes for (var i = 0; i < str.length; ++i) { var code = str.charCodeAt(i); if (code > 3424) { code -= 3424; } bytes[i] = code; } var maxChunk = 300; var j = 0; if (bytes.length > maxChunk) { for (var i = 0; i < bytes.length; i += maxChunk) { var subStr; if (i + maxChunk <= bytes.length) { subStr = copyByte(bytes, i, i + maxChunk); } else { subStr = copyByte(bytes, i, bytes.length); } setTimeout(writeStrToCharacteristic, 2000 * j, subStr); j++; } } else { writeStrToCharacteristic(bytes); } function copyByte(ori, i, n) { var len = n - i; var buff = new Uint8Array(len); var c = 0; for (var j = i; j <= n; j++) { buff[c] = ori[j]; c++ } return buff; } function writeStrToCharacteristic(buffer) { return $rootScope.printCharacteristic.writeValue(buffer); } } $scope.browseFile = function () { //$('#picFile').click(); } $scope.uploadProfilePictureFile = function () { return; try { $('.splash').css('display', 'block'); console.log(document.getElementById('picFile').files); var f = document.getElementById('picFile').files[0], r = new FileReader(); var type = document.getElementById('picFile').files[0].type; r.onloadend = function (e) { var data = e.target.result; $scope.display_transaction = false; $http( { method: 'POST', data: { file: btoa(data), type: type, id: $scope.profile.USER.UID, do: 'base.upload_profile_image' }, url: 'core/service.php' } ).then(function (response) { var a = new Date(); var t = a.getTime(); $scope.userProfileImage = "core/service.php?do=base.image&name=USER&id=" + $scope.profile.USER.UID + "&tmp=" + t; $('#picFile').val(null); $('.splash').css('display', 'none'); }); //send your binary data via $http or $resource or do anything else with it } r.readAsBinaryString(f); } catch (error) { swal("Please check your file again."); $('.splash').css('display', 'none'); } } $window.uploadProfilePictureFile = function () { $scope.uploadProfilePictureFile(); } // For iCheck purpose only $scope.checkOne = true; /** * Sparkline bar chart data and options used in under Profile image on left navigation panel */ $scope.barProfileData = [5, 6, 7, 2, 0, 4, 2, 4, 5, 7, 2, 4, 12, 11, 4]; $scope.barProfileOptions = { type: 'bar', barWidth: 7, height: '30px', barColor: '#62cb31', negBarColor: '#53ac2a' }; $scope.chartIncomeData = [ { label: "line", data: [[1, 10], [2, 26], [3, 16], [4, 36], [5, 32], [6, 51]] } ]; $scope.chartIncomeOptions = { series: { lines: { show: true, lineWidth: 0, fill: true, fillColor: "#64cc34" } }, colors: ["#62cb31"], grid: { show: false }, legend: { show: false } }; /** * Tooltips and Popover - used for tooltips in components view */ $scope.dynamicTooltip = 'Hello, World!'; $scope.htmlTooltip = "I\'ve been made bold!"; $scope.dynamicTooltipText = 'Dynamic'; $scope.dynamicPopover = 'Hello, World!'; $scope.dynamicPopoverTitle = 'Title'; /** * Pagination - used for pagination in components view */ $scope.totalItems = 64; $scope.currentPage = 4; $scope.setPage = function (pageNo) { $scope.currentPage = pageNo; }; /** * Typehead - used for typehead in components view */ $scope.states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Dakota', 'North Carolina', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming']; // Any function returning a promise object can be used to load values asynchronously $scope.getLocation = function (val) { return $http.get('http://maps.googleapis.com/maps/api/geocode/json', { params: { address: val, sensor: false } }).then(function (response) { return response.data.results.map(function (item) { return item.formatted_address; }); }); }; /** * Rating - used for rating in components view */ $scope.rate = 7; $scope.max = 10; $scope.hoveringOver = function (value) { $scope.overStar = value; $scope.percent = 100 * (value / this.max); }; /** * groups - used for Collapse panels in Tabs and Panels view */ $scope.groups = [ { title: 'Dynamic Group Header - 1', content: 'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. ' }, { title: 'Dynamic Group Header - 2', content: 'A wonderful serenity has taken possession of my entire soul, like these sweet mornings of spring which I enjoy with my whole heart. I am alone, and feel the charm of existence in this spot, which was created for the bliss of souls like mine. ' } ]; $scope.oneAtATime = true; /** * Some Flot chart data and options used in Dashboard */ var data1 = [[0, 55], [1, 48], [2, 40], [3, 36], [4, 40], [5, 60], [6, 50], [7, 51]]; var data2 = [[0, 56], [1, 49], [2, 41], [3, 38], [4, 46], [5, 67], [6, 57], [7, 59]]; $scope.chartUsersData = [data1, data2]; $scope.chartUsersOptions = { series: { splines: { show: true, tension: 0.4, lineWidth: 1, fill: 0.4 }, }, grid: { tickColor: "#f0f0f0", borderWidth: 1, borderColor: 'f0f0f0', color: '#6a6c6f' }, colors: ["#62cb31", "#efefef"], }; /** * Some Pie chart data and options */ $scope.PieChart = { data: [1, 5], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.PieChart2 = { data: [226, 360], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.PieChart3 = { data: [0.52, 1.561], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.PieChart4 = { data: [1, 4], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.PieChart5 = { data: [226, 134], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.PieChart6 = { data: [0.52, 1.041], options: { fill: ["#62cb31", "#edf0f5"] } }; $scope.BarChart = { data: [5, 3, 9, 6, 5, 9, 7, 3, 5, 2], options: { fill: ["#dbdbdb", "#62cb31"], } }; $scope.LineChart = { data: [5, 9, 7, 3, 5, 2, 5, 3, 9, 6, 5, 9, 4, 7, 3, 2, 9, 8, 7, 4, 5, 1, 2, 9, 5, 4, 7], options: { fill: '#62cb31', stroke: '#62cb31', width: 64 } }; $scope.stanimation = 'bounceIn'; $scope.runIt = true; $scope.runAnimation = function () { $scope.runIt = false; $timeout(function () { $scope.runIt = true; }, 100) }; //-------------------------------------------- App Function // Get all role in server $scope.getAllRole = function () { var allstate = $state.get(); var roles = []; var modules = []; for (var i = 0; i < allstate.length; i++) { if (allstate[i].data != undefined) { if (allstate[i].data.module == 'Y') { allstate[i].data.name = allstate[i].name; modules.push(allstate[i].data); for (var j = 0; j < allstate[i].data.role.length; j++) { roles.push(allstate[i].data.role[j]); } } } } global.set_modules(modules); global.set_roles(roles); $scope.modules = modules; $scope.roles = roles; } // Get all role in server $scope.createMenu = function () { var allstate = $state.get(); var menues = {}; for (var i = 0; i < allstate.length; i++) { if (allstate[i].data != undefined) { if (allstate[i].data.module == 'Y') { for (var j = 0; j < allstate[i].data.role.length; j++) { // console.log(allstate[i].data.role[j].name+" = "+$scope.profile.GROUP.GROUP_PROFILE[allstate[i].data.role[j].name]); if ($scope.profile.GROUP.GROUP_PROFILE[allstate[i].data.role[j].name] == 'Y') { for (var k = 0; k < allstate[i].data.role[j].access.length; k++) { if (allstate[i].data.menu[allstate[i].data.role[j].access[k]] != undefined) { var menu = { module: allstate[i].name, url: allstate[i].data.menu[allstate[i].data.role[j].access[k]].url, title: allstate[i].data.menu[allstate[i].data.role[j].access[k]].title, class: allstate[i].data.menu[allstate[i].data.role[j].access[k]].class, sidemenu: allstate[i].data.menu[allstate[i].data.role[j].access[k]].sidemenu, }; if (allstate[i].data.menu[allstate[i].data.role[j].access[k]].quickmenu == 'Y') { menu.quickmenu = allstate[i].data.menu[allstate[i].data.role[j].access[k]].quickmenu; menu.quick_label = allstate[i].data.menu[allstate[i].data.role[j].access[k]].quick_label; } menues[allstate[i].data.menu[allstate[i].data.role[j].access[k]].url] = menu; } } } } } } } global.set_menues(menues); $scope.menues = menues; // console.log($scope.profile.GROUP.GROUP_PROFILE); console.log(global.menues); } // Login $scope.login = function (login) { //console.log('Login'); var parameter = { LOGIN: 'YES', U: login.u, P: login.p }; var url = "core/service.php"; $http.post(url, parameter). success(function (data, status, headers, config) { if (data.STATUS == 'OK') { $scope.profile = data.DATA; if ($scope.profile.GROUP.GROUP_PROFILE == '') { $scope.profile.GROUP.GROUP_PROFILE = {}; } else { $scope.profile.GROUP.GROUP_PROFILE = JSON.parse($scope.profile.GROUP.GROUP_PROFILE); } $scope.createMenu(); global.set_profile($scope.profile); $scope.userProfileImage = "core/service.php?do=base.image&name=USER&id=" + $scope.profile.USER.UID; $scope.initNoti(); global.auth = 'Y'; console.log('Login success'); if ($scope.profile.GROUP.landing_url != '') { $rootScope.$state.go($scope.profile.GROUP.landing_url); } else { if ('smrc.app' != '') { $rootScope.$state.go('smrc.app'); } else { $rootScope.$state.go('dashboard'); } } } else { global.auth = 'N'; sweetAlert.swal({ title: '' + data.CODE, text: '' + data.MSG, type: "error" }); } }). error(function (data, status, headers, config) { global.auth = 'N'; sweetAlert.swal({ title: '' + status, text: '' + data, type: "error" }); }); } // Logout $scope.logout = function () { var parameter = { LOGOUT: 'YES' }; var url = "core/service.php"; $http.post(url, parameter). success(function (data, status, headers, config) { // this callback will be called asynchronously // when the response is available if (data.STATUS == 'OK') { $scope.profile = {}; global.auth = 'N'; $rootScope.$state.go('common.login'); } else { global.auth = 'N'; sweetAlert.swal({ title: '' + data.CODE, text: '' + data.MSG, type: "error" }); } }). error(function (data, status, headers, config) { global.auth = 'N'; sweetAlert.swal({ title: '' + status, text: '' + data, type: "error" }); // called asynchronously if an error occurs // or server returns response with an error status. }); } $scope.initNoti = function () { /* $scope.authObj = $firebaseAuth(); $scope.authObj.$signInWithEmailAndPassword("redkoma@gmail.com", "password").then(function (firebaseUser) { console.log("Signed in as:", firebaseUser.uid); if($scope.profile.USER.REMARK10!=''){ var picref = firebase.database().ref().child("wallet/"+$scope.profile.USER.REMARK10+"/pic"); var pic= $firebaseObject(picref); pic.$loaded() .then(function(data) { console.log(data); //$scope.userProfileImage=data.$value; }) .catch(function(error) { console.error("Error:", error); }); } */ /* var ref = firebase.database().ref().child(appid+"/u"+$scope.profile.USER.UID+"/inbox"); $scope.inbox = $firebaseArray(ref); var ref1 = firebase.database().ref().child(appid+"/u"+$scope.profile.USER.UID+"/new_inbox"); $scope.new_inbox = $firebaseArray(ref1); var ref2 = firebase.database().ref().child(appid+"/u"+$scope.profile.USER.UID+"/alert"); $scope.notification = $firebaseArray(ref2); $scope.new_inbox.$watch(function() { if($scope.new_inbox!=undefined){ if($scope.new_inbox.length>0){ notify({ message: $scope.new_inbox[0].text, classes: 'alert-info' }); $scope.inbox.$add(angular.copy($scope.new_inbox[0])); $scope.new_inbox.$remove(0); } } }); var ref3 = firebase.database().ref().child("wallet/"+$scope.profile.USER.USER_PROFILE_CODE); $scope.wallet = $firebaseObject(ref3); global.set_para('wallet',$scope.wallet); */ /* }).catch(function (error) { console.error("Authentication failed:", error); }); */ /* var ref = firebase.database().ref().child(appid+"/"+$scope.profile.USER.USERNAME); var syncObject = $firebaseObject(ref); syncObject.$bindTo($scope, "noti"); syncObject.$watch(function() { console.log("changed"); if($scope.noti!=undefined){ if($scope.noti.inbox==undefined){ $scope.noti.inbox=[]; } if($scope.noti.new_inbox!=undefined){ if($scope.noti.new_inbox.length>0){ for(var i=0;i<$scope.noti.new_inbox.length;i++){ notify({ message: $scope.noti.new_inbox[i].text, classes: 'alert-info' }); $scope.noti.inbox.push(angular.copy($scope.noti.new_inbox[i])); } $scope.noti.new_inbox=[]; } } } }); */ } $scope.addNoti = function (msg, link) { global.addNoti(msg, link); } $scope.addAlert = function (msg, type) { global.addAlert(msg, type); } $scope.clearNoti = function () { for (var i = 0; i < $scope.inbox.length; i++) { $scope.inbox.$remove(i); } } // Auth $scope.auth = function () { var parameter = { do: 'base.session' }; var url = "core/service.php"; $http.post(url, parameter). success(function (data, status, headers, config) { // this callback will be called asynchronously // when the response is available if (data.STATUS == 'OK') { $scope.profile = data.DATA; if ($scope.profile.GROUP.GROUP_PROFILE == '') { $scope.profile.GROUP.GROUP_PROFILE = {}; } else { $scope.profile.GROUP.GROUP_PROFILE = JSON.parse($scope.profile.GROUP.GROUP_PROFILE); } $scope.createMenu(); global.set_profile($scope.profile); global.auth = 'Y'; $scope.userProfileImage = "core/service.php?do=base.image&name=USER&id=" + $scope.profile.USER.UID; $scope.initNoti(); /* if($scope.profile.GROUP.landing_url != ''){ $rootScope.$state.go($scope.profile.GROUP.landing_url); }else{ if('smrc.app'!='') { $rootScope.$state.go('smrc.app'); }else{ $rootScope.$state.go('dashboard'); } } */ } else { global.auth = 'N'; $rootScope.$state.go('landing'); } }). error(function (data, status, headers, config) { global.auth = 'N'; sweetAlert.swal({ title: status, text: data, type: "error" }); $rootScope.$state.go('landing'); // called asynchronously if an error occurs // or server returns response with an error status. }); } $scope.getAllRole(); $scope.auth(); //--* EXTEND_CODE_MODULES LOADED *--// }