angular.module('app', ['ionic'])
.factory('DataTree', function() {
return {
all: function(trunkName) {
var trunkString = window.localStorage[trunkName];
if (trunkString) {
return angular.fromJson(trunkString);
}
return [];
},
save: function(trunkName, trunkString) {
//console.log(trunkString);
window.localStorage[trunkName] = angular.toJson(trunkString);
}
}
})
.controller('homeCtrl', ['$scope', '$stateParams', '$ionicModal', 'DataTree',
function($scope, $stateParams, $ionicModal, DataTree) {}
])
.controller('menuCtrl', ['$scope', '$stateParams',
function($scope, $stateParams) {}
])
.controller('monthlyCtrl', ['$scope', '$stateParams', 'DataTree',
function($scope, $stateParams, DataTree) {
var titleSource = "inventoryRecordBookDetails";
var listRecords = [];
var listSummaryIndex = [];
var listSummaryRecords = [];
function summarizeMonthly() {
listRecords = DataTree.all(titleSource);
// init lists
listSummaryIndex = [];
listSummaryRecords = [];
// process listDetailRecords
listRecords.forEach(function(d) {
//console.log(d);
d.date = new Date(d.date);
yearmonthvalue = d.date.toISOString().substring(0, 7);
var obj = {
yearmonth: yearmonthvalue,
desc: d.desc,
cost: d.cost,
sale: d.sale
};
//assign arrayData item to lists
var i = listSummaryIndex.indexOf(yearmonthvalue);
if (i < 0) {
listSummaryIndex.push(yearmonthvalue);
listSummaryRecords.push(obj);
} else {
listSummaryRecords[i].cost =
parseFloat(listSummaryRecords[i].cost) +
parseFloat(obj.cost);
listSummaryRecords[i].sale =
parseFloat(listSummaryRecords[i].sale) +
parseFloat(obj.sale);
}
//console.log(listSummaryIndex);
//console.log(listSummaryRecords);
});
//console.log(listSummaryRecords);
}
function refreshView() {
summarizeMonthly();
$scope.listSummaryIndex = listSummaryIndex;
$scope.listSummaryRecords = listSummaryRecords;
}
$scope.$on('$ionicView.enter', function() {
refreshView();
});
}
])
.controller('dailyCtrl', ['$scope', '$stateParams', '$ionicModal', 'DataTree',
function($scope, $stateParams, $ionicModal, DataTree) {
$scope.entry = {};
$scope.yearmonths = $stateParams.param1;
$scope.selectedyearmonth = $stateParams.param2;
var titleSource = "inventoryRecordBookDetails";
var listRecords = [];
var listSummaryIndex = [];
var listSummaryRecords = [];
function summarizeDaily() {
listRecords = DataTree.all(titleSource);
// init lists
listSummaryIndex = [];
listSummaryRecords = [];
// process listRecords
listRecords.forEach(function(d) {
//console.log(d);
d.date = new Date(d.date);
e = d.date;
e = e.getTime();
yearmonthvalue = d.date.toISOString().substring(0, 7);
var obj = {
yearmonth: yearmonthvalue,
date: e,
cost: d.cost,
sale: d.sale
};
//assign arrayData item to lists
if (yearmonthvalue == $scope.entry.yearmonth) {
//console.log(obj.date);
//console.log(listSummaryIndex);
var i = listSummaryIndex.indexOf(obj.date);
//console.log(i);
if (i < 0) {
listSummaryIndex.push(obj.date);
listSummaryRecords.push(obj);
} else {
listSummaryRecords[i].cost =
parseFloat(listSummaryRecords[i].cost) +
parseFloat(obj.cost);
listSummaryRecords[i].sale =
parseFloat(listSummaryRecords[i].sale) +
parseFloat(obj.sale);
}
}
//console.log(listSummaryIndex);
//console.log(listSummaryRecords);
});
//console.log(listSummaryRecords);
}
$scope.refreshView = function() {
summarizeDaily();
$scope.listSummaryRecords = listSummaryRecords;
}
$scope.$on('$ionicView.enter', function() {
$scope.refreshView();
});
}
])
.controller('detailsCtrl', ['$scope', '$stateParams', '$ionicModal', '$ionicPopup', 'DataTree',
function($scope, $stateParams, $ionicModal, $ionicPopup, DataTree) {
$scope.entry = {};
var titleDetails = "inventoryRecordBookDetails";
var sourceDetails = DataTree.all(titleDetails);
// Create and load the Modal
$ionicModal.fromTemplateUrl('insertRecord.html', function(modal) {
$scope.insertRecordModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when the form is submitted
$scope.submitInsertRecord = function(entry) {
//convert the date into UTC Date
//to avoid time zone differences
var d = new Date(entry.date);
var utcDate = new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(), 0, 0, 0));
// add new item to data source
sourceDetails.push({
date: utcDate,
desc: entry.desc,
cost: entry.cost,
sale: entry.sale
});
//sort data source by date in ascending order
sourceDetails.sort(function(a, b) {
return new Date(a.date).getTime() -
new Date(b.date).getTime()
});
//save data source into storage
DataTree.save(titleDetails, sourceDetails);
$scope.Details = sourceDetails;
//if search date entry exists
//perform searchDate()
$scope.entry.searchDate = $scope.entry.date;
$scope.refreshView();
//reset entry value
$scope.entry.desc = null;
$scope.entry.cost = null;
$scope.entry.sale = null;
$scope.insertRecordModal.hide();
};
// Open our new task modal
$scope.insertRecord = function(item) {
//if searchDate is not null get its value
//else, get current date value
if ($scope.entry.searchDate == null) {
$scope.entry.date = new Date();
} else {
$scope.entry.date = $scope.entry.searchDate;
}
$scope.entry.desc = item.desc;
$scope.entry.cost = item.cost;
$scope.entry.sale = item.sale;
$scope.insertRecordModal.show();
};
// Close the new task modal
$scope.closeInsertRecord = function() {
$scope.insertRecordModal.hide();
};
// Create and load the Modal
$ionicModal.fromTemplateUrl('selectCategory.html', function(modal) {
$scope.selectCategoryModal = modal;
}, {
scope: $scope,
animation: 'slide-in-up'
});
// Called when the form is submitted
$scope.submitCategory = function(entry) {
$scope.CategoryItems = [];
$scope.selectCategoryModal.hide();
$scope.insertRecord(entry);
}
// Open our new Category Selection modal
$scope.selectCategory = function() {
$scope.selectCategoryModal.show();
};
// Close the new task modal
$scope.closeSelectCategory = function() {
$scope.CategoryItems = [];
$scope.selectCategoryModal.hide();
};
$scope.getCategoryItems = function(category) {
$scope.CategoryItems = [{
desc: 'Cat ' + category + ' Item 1',
cost: 10 * category,
sale: 20 * category
},
{
desc: 'Cat ' + category + ' Item 2',
cost: 10 * category,
sale: 20 * category
},
{
desc: 'Cat ' + category + ' Item 3',
cost: 10 * category,
sale: 20 * category
}
]
}
$scope.refreshView = function() {
//if input is null
if ($scope.entry.searchDate == null) {
$scope.details = [];
return;
} else { //if input is not null
var d = new Date($scope.entry.searchDate);
var testDate = new Date(Date.UTC(
d.getFullYear(),
d.getMonth(),
d.getDate(), 0, 0, 0));
var obj = sourceDetails.filter(function(obj) {
var sourceDate = new Date(obj.date);
//console.log(sourceDate.getTime()+"="+utcDate.getTime());
return (sourceDate.getTime() === testDate.getTime());
//return obj.date === $scope.detailsPage.inputDate;
});
//console.log(obj);
$scope.details = obj;
}
}
$scope.$on('$ionicView.enter', function() {
$scope.entry.searchDate = (
$stateParams.param1 == null ?
new Date() : new Date($stateParams.param1));
if ($stateParams.param2 == '1') {
$scope.selectCategory();
}
$scope.refreshView();
});
}
])
.controller('settingsCtrl', ['$scope', '$stateParams',
function($scope, $stateParams) {}
])
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('menu.home', {
url: '/page1',
views: {
'side-menu21': {
templateUrl: 'home.html',
controller: 'homeCtrl'
}
}
})
.state('menu', {
url: '/side-menu21',
templateUrl: 'menu.html',
controller: 'menuCtrl'
})
.state('menu.monthly', {
url: '/page2',
views: {
'side-menu21': {
templateUrl: 'monthly.html',
controller: 'monthlyCtrl'
}
}
})
.state('menu.daily', {
url: '/page3',
params: {
param1: null,
param2: null
},
views: {
'side-menu21': {
templateUrl: 'daily.html',
controller: 'dailyCtrl'
}
}
})
.state('menu.details', {
url: '/page4',
params: {
param1: null,
param2: null
},
views: {
'side-menu21': {
templateUrl: 'details.html',
controller: 'detailsCtrl'
}
}
})
.state('menu.settings', {
url: '/page5',
views: {
'side-menu21': {
templateUrl: 'settings.html',
controller: 'settingsCtrl'
}
}
})
$urlRouterProvider.otherwise('/side-menu21/page1')
});
|