diff --git a/www/js/controllers.js b/www/js/controllers.js index 4819b91..acbc0ad 100644 --- a/www/js/controllers.js +++ b/www/js/controllers.js @@ -36,7 +36,7 @@ MIM.controller('ConfigController', function ($ionicPlatform, $ionicLoading, $loc }); }); -MIM.controller('SalesController', function ($scope, $ionicPlatform, $cordovaSQLite, $ionicPopup, Customer) { +MIM.controller('SalesController', function ($scope, $ionicPopup, Customer, Product, Sale) { $scope.customers = []; $scope.products = []; @@ -44,57 +44,29 @@ MIM.controller('SalesController', function ($scope, $ionicPlatform, $cordovaSQLi $scope.customers = customers; }); - var productQuery = 'SELECT id, name FROM Products WHERE remaining_amount > 0'; - $cordovaSQLite.execute(db, productQuery, []).then(function (res) { - if (res.rows.length) { - for (var i = 0; i < res.rows.length; i++) { - $scope.products.push({ - id: res.rows.item(i).id, - product_name: res.rows.item(i).name, - }); - } - } - }, function (error) { - console.error(error); + Product.hasAmount().then(function (products) { + $scope.products = products; }); - $scope.addSale = function (saleData) { - var transactQuery = 'INSERT INTO Transactions (categories, total_price, ' + - 'status, customer_id) VALUES (?, ?, ?, ?)'; - var buyQuery = 'INSERT INTO Buying (transaction_id, product_id, amount) VALUES (?, ?, ?)'; - $cordovaSQLite.execute(db, transactQuery, ["P", saleData.total_price, "1", saleData.customers]).then(function (tx) { - $cordovaSQLite.execute(db, buyQuery, [tx.insertId, saleData.products, saleData.amount]).then(function (res) { - console.log('Customer id ' + saleData.customers + ' and Transaction id ' + - tx.insertId + ' are successfully inserted.'); - $scope.getRemainingAmount(saleData.products, saleData.amount); - $scope.updateProductAmount(saleData.products, saleData.amount); - $scope.showAlert(); - saleData.newItem = ''; - }, function (error) { - console.error(error); - }); + $scope.addSale = function(saleData) { + Sale.addTransaction(saleData).then(function (res) { + Sale.addBuying(res.insertId, saleData); + $scope.getRemainingAmount(saleData.products, saleData.amount); + $scope.updateProductAmount(saleData.products, saleData.amount); + $scope.showAlert(); + saleData.newItem = ''; }); }; - $scope.getRemainingAmount = function (id, amount) { - var query = 'SELECT remaining_amount FROM Products WHERE id = ?'; - $cordovaSQLite.execute(db, query, [id]).then(function (res) { - if (res.rows.length) { - $scope.remaining_amount = res.rows.item(0).remaining_amount - amount; - $scope.updateProductAmount(id, $scope.remaining_amount); - } - }, function (error) { - console.error(error); + $scope.getRemainingAmount = function(id, amount) { + Product.getAmount(id).then(function (productAmount) { + $scope.remaining_amount = productAmount.remaining_amount - amount; + $scope.updateProductAmount(id, $scope.remaining_amount); }); }; - $scope.updateProductAmount = function (id, remaining_amount) { - var query = 'UPDATE Products SET remaining_amount = ? WHERE id = ?'; - $cordovaSQLite.execute(db, query, [remaining_amount, id]).then(function (res) { - console.log('one row is affected'); - }, function (error) { - console.error(error); - }); + $scope.updateProductAmount = function(id, remaining_amount) { + Product.updateAmount(id, remaining_amount); }; $scope.showAlert = function () { @@ -102,13 +74,14 @@ MIM.controller('SalesController', function ($scope, $ionicPlatform, $cordovaSQLi title: 'Success', template: 'A new transaction has been added', }); + alertPopup.then(function (res) { console.log('Transaction is successfully added.'); }); }; }); -MIM.controller('SalesOrderController', function ($scope, $ionicPlatform, $cordovaSQLite, $ionicModal, $ionicPopup, Customer, Product) { +MIM.controller('SalesOrderController', function ($scope, $ionicModal, $ionicPopup, Customer, Product, Order) { $scope.customers = []; $scope.products = []; $scope.orders = []; @@ -121,43 +94,26 @@ MIM.controller('SalesOrderController', function ($scope, $ionicPlatform, $cordov $scope.products = products; }); - var query = 'SELECT t.id AS id, c.name AS name FROM Transactions t, Customers c ' + - 'WHERE categories = ? AND t.customer_id = c.id'; - $cordovaSQLite.execute(db, query, ["O"]).then(function (res) { - if (res.rows.length) { - for (var i = 0; i < res.rows.length; i++) { - $scope.orders.push({ - id: res.rows.item(i).id, - customer_name: res.rows.item(i).name, - }); - } - } - }, function (error) { - console.error(error); + $scope.$on('$ionicView.enter', function () { + $scope.populateOrders(); + }); + + $scope.populateOrders = function () { + Order.all('O').then(function (orders) { + $scope.orders = orders; }); + }; - $scope.addOrder = function (ordersData) { - var transactQuery = 'INSERT INTO Transactions (categories, total_price, ' + - 'status, customer_id) VALUES (?, ?, ?, ?)'; - var orderQuery = 'INSERT INTO Buying (transaction_id, product_id, amount) VALUES (?, ?, ?)'; - var query = 'SELECT name FROM Customers WHERE id = ?'; - $cordovaSQLite.execute(db, transactQuery, ["O", ordersData.total_price, "0", ordersData.customers]).then(function (tx) { - $cordovaSQLite.execute(db, orderQuery, [tx.insertId, ordersData.products, ordersData.amount]).then(function (res) { - $cordovaSQLite.execute(db, query, [ordersData.customers]).then(function (res) { - if (res.rows.length) { - $scope.orders.push({ - id: tx.insertId, - customer_name: res.rows.item(0).name, - }); - ordersData.newItem = ''; - $scope.closeOrderModal(); - console.log('Customer id ' + ordersData.customers + ' and ' + - 'Transaction id ' + tx.insertId + ' are successfully inserted.'); - } - }, function (error) { - console.error(error); - }); + $scope.addOrder = function(ordersData) { + Order.addTransaction(ordersData).then(function (res) { + Order.addOrder(res.insertId, ordersData); + Customer.get(ordersData.customers).then(function (customers) { + console.log('Customer id ' + ordersData.customers + ' and ' + + 'Transaction id ' + res.insertId + ' are successfully inserted.'); }); + $scope.populateOrders(); + $scope.closeOrderModal(); + ordersData.newItem = ''; }); }; @@ -185,17 +141,12 @@ MIM.controller('SalesOrderController', function ($scope, $ionicPlatform, $cordov title: 'Delete an Order', template: 'Are you sure you want to delete this sale order?', }); + confirmPopup.then(function (res) { if (res) { - var outerQuery = 'DELETE FROM Buying WHERE transaction_id = ?'; - var innerQuery = 'DELETE FROM Transactions WHERE id = ?'; - $cordovaSQLite.execute(db, outerQuery, [order.id]).then(function (tx) { - $cordovaSQLite.execute(db, innerQuery, [order.id]).then(function (tx) { - $scope.orders.splice($scope.orders.indexOf(order), 1); - }); - }, function (error) { - console.error(error); - }); + Order.deleteBuying(order.id); + Order.deleteTransaction(order.id); + $scope.populateOrders(); } else { console.log('You cancel deleting this sale order.'); } @@ -203,34 +154,19 @@ MIM.controller('SalesOrderController', function ($scope, $ionicPlatform, $cordov }; }); -MIM.controller('OrderDetailController', function ($scope, $ionicPlatform, $cordovaSQLite, $stateParams) { - $ionicPlatform.ready(function () { - var query = 'SELECT p.name AS name, b.amount AS amount, t.total_price ' + - 'AS total_price FROM Transactions t, Buying b, Products p WHERE ' + - 't.id = b.transaction_id AND b.product_id = p.id AND t.id = ?'; - $cordovaSQLite.execute(db, query, [$stateParams.orderId]).then(function (res) { - if (res.rows.length) { - $scope.product_name = res.rows.item(0).name; - $scope.order_amount = res.rows.item(0).amount; - $scope.order_price = res.rows.item(0).total_price; - } - }, function (error) { - console.error(error); - }); +MIM.controller('OrderDetailController', function ($scope, $stateParams, Order) { + Order.get($stateParams.orderId).then(function (orderDetail) { + $scope.product_name = orderDetail.name; + $scope.order_amount = orderDetail.amount; + $scope.order_price = orderDetail.total_price; }); }); -MIM.controller('AddInventoryController', function ($scope, $cordovaSQLite, $ionicPopup) { - $scope.addProduct = function (productData) { - var query = 'INSERT INTO Products (name, description, remaining_amount, ' + - 'selling_price, purchase_price) VALUES (?, ?, ?, ?, ?)'; - $cordovaSQLite.execute(db, query, [productData.name, productData.description, productData.amount, productData.selling_price, productData.purchase_price]).then(function (res) { - console.log('Item ' + res.insertId + ' is successfully inserted.'); - $scope.showAlert(); - productData.newItem = ''; - }, function (error) { - console.error(error); - }); +MIM.controller('AddInventoryController', function ($scope, $ionicPopup, Product) { + $scope.addProduct = function(productData) { + Product.add(productData); + $scope.showAlert(); + productData.newItem = ''; }; $scope.showAlert = function () { @@ -238,43 +174,33 @@ MIM.controller('AddInventoryController', function ($scope, $cordovaSQLite, $ioni title: 'Success', template: 'A new item has been added', }); + alertPopup.then(function (res) { console.log('Item is successfully inserted.'); }); }; }); -MIM.controller('InventoryItemsController', function ($scope, $ionicPlatform, $cordovaSQLite, $ionicModal, $ionicPopup) { +MIM.controller('InventoryItemsController', function ($scope, $ionicModal, $ionicPopup, Product) { $scope.inventory = []; - $ionicPlatform.ready(function () { - var query = 'SELECT id, name, remaining_amount FROM Products ORDER BY remaining_amount DESC'; - $cordovaSQLite.execute(db, query, []).then(function (res) { - if (res.rows.length) { - for (var i = 0; i < res.rows.length; i++) { - $scope.inventory.push({ - id: res.rows.item(i).id, - product_name: res.rows.item(i).name, - product_amount: res.rows.item(i).remaining_amount, - }); - } - } - }, function (error) { - console.error(error); - }); + + $scope.$on('$ionicView.enter', function () { + $scope.populateProducts(); }); - $scope.editItem = function (productData) { - var query = 'UPDATE Products SET name = ?, description = ?, ' + - 'selling_price = ?, purchase_price = ?, updated_at = DATETIME(\'now\') WHERE id = ?'; - $cordovaSQLite.execute(db, query, [productData.name, productData.description, productData.selling_price, productData.purchase_price, productData.id]).then(function (res) { - console.log('Item ' + productData.id + ' is updated.'); - productData.newItem = ''; - $scope.closeItemModal(); - }, function (error) { - console.error(error); + $scope.populateProducts = function () { + Product.orderByAmount().then(function (products) { + $scope.inventory = products; }); }; + $scope.editItem = function(productData) { + Product.update(productData); + $scope.populateProducts(); + $scope.closeItemModal(); + $scope.cleanForm(); + }; + $ionicModal.fromTemplateUrl('templates/edit_item.html', { scope: $scope, animation: 'slide-in-up', @@ -284,20 +210,16 @@ MIM.controller('InventoryItemsController', function ($scope, $ionicPlatform, $co $scope.openItemModal = function (item) { $scope.productData = {}; - var query = 'SELECT id, name, description, remaining_amount, selling_price, ' + - 'purchase_price FROM Products WHERE id = ?'; - $cordovaSQLite.execute(db, query, [item.id]).then(function (res) { - if (res.rows.length) { - $scope.productData.id = res.rows.item(0).id; - $scope.productData.name = res.rows.item(0).name; - $scope.productData.description = res.rows.item(0).description; - $scope.productData.amount = res.rows.item(0).remaining_amount; - $scope.productData.purchase_price = res.rows.item(0).purchase_price; - $scope.productData.selling_price = res.rows.item(0).selling_price; - } - }, function (error) { - console.error(error); + + Product.get(item.id).then(function (itemData) { + $scope.productData.id = itemData.id; + $scope.productData.name = itemData.name; + $scope.productData.description = itemData.description; + $scope.productData.amount = itemData.remaining_amount; + $scope.productData.purchase_price = itemData.purchase_price; + $scope.productData.selling_price = itemData.selling_price; }); + $scope.modal.show(); }; @@ -305,6 +227,10 @@ MIM.controller('InventoryItemsController', function ($scope, $ionicPlatform, $co $scope.modal.hide(); }; + $scope.cleanForm = function () { + productData.newItem = ''; + }; + $scope.$on('$destroy', function () { $scope.modal.remove(); }); @@ -314,14 +240,11 @@ MIM.controller('InventoryItemsController', function ($scope, $ionicPlatform, $co title: 'Delete an Item', template: 'Are you sure you want to delete this item?', }); + confirmPopup.then(function (res) { if (res) { - var query = 'DELETE FROM Products WHERE id = ?'; - $cordovaSQLite.execute(db, query, [item.id]).then(function (tx) { - $scope.inventory.splice($scope.inventory.indexOf(item), 1); - }, function (error) { - console.error(error); - }); + Product.delete(item); + $scope.populateProducts(); } else { console.log('You cancel deleting this item.'); } @@ -329,61 +252,43 @@ MIM.controller('InventoryItemsController', function ($scope, $ionicPlatform, $co }; }); -MIM.controller('ItemDetailController', function ($scope, $ionicPlatform, $cordovaSQLite, $stateParams) { - $ionicPlatform.ready(function () { - var query = 'SELECT name, description, remaining_amount, selling_price, ' + - 'purchase_price, DATETIME(created_at, \'localtime\') AS created_date, ' + - 'DATETIME(updated_at, \'localtime\') AS updated_date ' + - 'FROM Products WHERE id = ?'; - $cordovaSQLite.execute(db, query, [$stateParams.itemId]).then(function (res) { - if (res.rows.length) { - $scope.product_name = res.rows.item(0).name; - $scope.product_description = res.rows.item(0).description; - $scope.product_amount = res.rows.item(0).remaining_amount; - $scope.purchase_price = res.rows.item(0).selling_price; - $scope.selling_price = res.rows.item(0).purchase_price; - $scope.created_date = res.rows.item(0).created_date; - $scope.updated_date = res.rows.item(0).updated_date; - } - }, function (error) { - console.error(error); - }); +MIM.controller('ItemDetailController', function ($scope, $stateParams, Product) { + Product.get($stateParams.itemId).then(function (itemDetail) { + $scope.product_name = itemDetail.name; + $scope.product_description = itemDetail.description; + $scope.product_amount = itemDetail.remaining_amount; + $scope.purchase_price = itemDetail.selling_price; + $scope.selling_price = itemDetail.purchase_price; + $scope.created_date = itemDetail.created_date; + $scope.updated_date = itemDetail.updated_date; }); }); -MIM.controller('CustomerController', function ($scope, $ionicPlatform, $cordovaSQLite, $ionicModal, $ionicPopup, Customer) { +MIM.controller('CustomerController', function ($scope, $ionicModal, $ionicPopup, Customer) { $scope.customers = []; - Customer.all().then(function (customers) { - $scope.customers = customers; + $scope.$on('$ionicView.enter', function () { + $scope.populateCustomers(); }); - $scope.addCustomer = function (customersData) { - var query = 'INSERT INTO Customers (name, address, telephone_number) VALUES (?, ?, ?)'; - $cordovaSQLite.execute(db, query, [customersData.name, customersData.address, customersData.phone]).then(function (res) { - $scope.customers.push({ - id: res.insertId, - customer_name: customersData.name, - customer_address: customersData.address, - customer_phone: customersData.phone, - }); - customersData.newItem = ''; - $scope.closeCustomerModal(1); - }, function (error) { - console.error(error); + $scope.populateCustomers = function () { + Customer.all().then(function(customers) { + $scope.customers = customers; }); }; - $scope.editCustomer = function (customersData) { - var query = 'UPDATE Customers SET name = ?, address = ?, telephone_number = ?, ' + - 'updated_at = DATETIME(\'now\') WHERE id = ?'; - $cordovaSQLite.execute(db, query, [customersData.name, customersData.address, customersData.phone, customersData.id]).then(function (res) { - console.log('Item ' + customersData.id + ' is updated.'); - customersData.newItem = ''; - $scope.closeCustomerModal(2); - }, function (error) { - console.error(error); - }); + $scope.addCustomer = function(customersData) { + Customer.add(customersData); + $scope.populateCustomers(); + $scope.closeCustomerModal(1); + $scope.cleanForm(); + }; + + $scope.editCustomer = function(customersData) { + Customer.update(customersData); + $scope.populateCustomers(); + $scope.closeCustomerModal(2); + $scope.cleanForm(); }; $ionicModal.fromTemplateUrl('templates/add_customer.html', { @@ -426,6 +331,10 @@ MIM.controller('CustomerController', function ($scope, $ionicPlatform, $cordovaS } }; + $scope.cleanForm = function () { + customersData.newItem = ''; + }; + $scope.$on('$destroy', function () { $scope.addModal.remove(); $scope.editModal.remove(); @@ -436,14 +345,11 @@ MIM.controller('CustomerController', function ($scope, $ionicPlatform, $cordovaS title: 'Delete Customer', template: 'Are you sure you want to delete this customer?', }); + confirmPopup.then(function (res) { if (res) { - var query = 'DELETE FROM Customers WHERE id = ?'; - $cordovaSQLite.execute(db, query, [customer.id]).then(function (tx) { - $scope.customers.splice($scope.customers.indexOf(customer), 1); - }, function (error) { - console.error(error); - }); + Customer.delete(customer); + $scope.populateCustomers(); } else { console.log('You cancel deleting this customer.'); } @@ -461,41 +367,35 @@ MIM.controller('CustomerDetailController', function ($scope, $stateParams, Custo }); }); -MIM.controller('StatisticsController', function ($scope, $ionicPlatform, $cordovaSQLite) { +MIM.controller('StatisticsController', function ($scope, Statistic) { $scope.month_year = []; $scope.monthly_transaction = []; $scope.monthly_income = []; $scope.series = ["month-year"]; $scope.count = []; $scope.total_price = []; - $ionicPlatform.ready(function () { - var transactionQuery = 'SELECT strftime(\'%m-%Y\', date) AS month_year, COUNT(id) AS total_transaction ' + - 'FROM Transactions GROUP BY month_year'; - $cordovaSQLite.execute(db, transactionQuery, []).then(function (res) { - if (res.rows.length) { - for (var i = 0; i < res.rows.length; i++) { - $scope.month_year.push(res.rows.item(i).month_year); - $scope.count.push(res.rows.item(i).total_transaction); - } - } - }, function (error) { - console.error(error); - }); + $scope.$on('$ionicView.enter', function () { + $scope.drawTransactionStat(); $scope.monthly_transaction.push($scope.count); + $scope.drawIncomeStat(); + $scope.monthly_income.push($scope.total_price); + }); - var incomeQuery = 'SELECT strftime(\'%m-%Y\', date) AS month_year, SUM(total_price) AS total_price ' + - 'FROM Transactions GROUP BY month_year'; - $cordovaSQLite.execute(db, incomeQuery, []).then(function (res) { - if (res.rows.length) { - for (var i = 0; i < res.rows.length; i++) { - $scope.total_price.push(res.rows.item(i).total_price); - } - } - }, function (error) { - console.error(error); - }); + $scope.drawTransactionStat = function () { + Statistic.getTransaction().then(function (res) { + for (var i = 0; i < res.length; i++) { + $scope.month_year.push(res[i].month_year); + $scope.count.push(res[i].total_transaction); + } + }); + }; - $scope.monthly_income.push($scope.total_price); - }); + $scope.drawIncomeStat = function () { + Statistic.getIncome().then(function (res) { + for (var i = 0; i < res.length; i++) { + $scope.total_price.push(res[i].total_price); + } + }); + }; }); diff --git a/www/js/services.js b/www/js/services.js index 6729c21..b6b53c2 100644 --- a/www/js/services.js +++ b/www/js/services.js @@ -59,6 +59,23 @@ MIM.factory('Customer', function ($cordovaSQLite, DB) { }); }; + this.add = function(customer) { + var parameters = [customer.name, customer.address, customer.phone]; + return DB.queryStatement("INSERT INTO Customers (name, address, telephone_number) " + + "VALUES (?, ?, ?)", parameters); + }; + + this.update = function (customer) { + var parameters = [customer.name, customer.address, customer.phone, customer.id]; + return DB.queryStatement('UPDATE Customers SET name = ?, address = ?, telephone_number = ?, ' + + 'updated_at = DATETIME(\'now\') WHERE id = ?', parameters); + }; + + this.delete = function (customer) { + var parameters = [customer.id]; + return DB.queryStatement('DELETE FROM Customers WHERE id = ?', parameters); + }; + return this; }); @@ -69,5 +86,168 @@ MIM.factory('Product', function ($cordovaSQLite, DB) { }); }; + this.hasAmount = function () { + return DB.queryStatement('SELECT id, name FROM Products WHERE remaining_amount > 0').then(function (res) { + return DB.getAll(res); + }); + }; + + this.get = function (productId) { + var parameters = [productId]; + return DB.queryStatement('SELECT id, name, description, remaining_amount, ' + + 'selling_price, purchase_price, DATETIME(created_at, \'localtime\') AS ' + + 'created_date, DATETIME(updated_at, \'localtime\') AS updated_date ' + + 'FROM Products WHERE id = ?', parameters).then(function (res) { + return DB.getById(res); + }); + }; + + this.getAmount = function (productId) { + var parameters = [productId]; + return DB.queryStatement('SELECT remaining_amount FROM Products WHERE id = ?', parameters).then(function (res) { + return DB.getById(res); + }); + }; + + this.add = function (product) { + var parameters = [ + product.name, + product.description, + product.amount, + product.selling_price, + product.purchase_price, + ]; + return DB.queryStatement('INSERT INTO Products (name, description, remaining_amount, ' + + 'selling_price, purchase_price) VALUES (?, ?, ?, ?, ?)', parameters); + }; + + this.update = function (product) { + var parameters = [ + product.name, + product.description, + product.selling_price, + product.purchase_price, product.id, + ]; + return DB.queryStatement('UPDATE Products SET name = ?, description = ?, ' + + 'selling_price = ?, purchase_price = ?, updated_at = DATETIME(\'now\') ' + + 'WHERE id = ?', parameters); + }; + + this.updateAmount = function (productId, productAmount) { + var parameters = [productAmount, productId]; + return DB.queryStatement('UPDATE Products SET remaining_amount = ? WHERE id = ?', parameters); + }; + + this.delete = function (product) { + var parameters = [product.id]; + return DB.queryStatement('DELETE FROM Products WHERE id = ?', parameters); + }; + + this.orderByAmount = function () { + return DB.queryStatement('SELECT id, name, remaining_amount FROM Products ' + + 'ORDER BY remaining_amount DESC').then(function (res) { + return DB.getAll(res); + }); + }; + + return this; +}); + +MIM.factory('Order', function ($cordovaSQLite, DB) { + this.all = function (orderCategory) { + var parameters = [orderCategory]; + return DB.queryStatement('SELECT t.id AS id, c.name AS name FROM Transactions t, ' + + 'Customers c WHERE categories = ? AND t.customer_id = c.id', parameters) + .then(function (res) { + return DB.getAll(res); + }); + }; + + this.get = function (orderId) { + var parameters = [orderId]; + return DB.queryStatement('SELECT p.name AS name, b.amount AS amount, ' + + 't.total_price AS total_price FROM Transactions t, Buying b, Products p ' + + 'WHERE t.id = b.transaction_id AND b.product_id = p.id AND t.id = ?', parameters) + .then(function (res) { + return DB.getById(res); + }); + }; + + this.addTransaction = function (ordersData) { + var parameters = [ + 'O', + ordersData.total_price, + '0', + ordersData.customers, + ]; + return DB.queryStatement('INSERT INTO Transactions (categories, total_price, ' + + 'status, customer_id) VALUES (?, ?, ?, ?)', parameters); + }; + + this.addOrder = function (transactionId, ordersData) { + var parameters = [ + transactionId, + ordersData.products, + ordersData.amount, + ]; + return DB.queryStatement('INSERT INTO Buying (transaction_id, product_id, ' + + 'amount) VALUES (?, ?, ?)', parameters); + }; + + this.deleteBuying = function (orderId) { + var parameters = [orderId]; + return DB.queryStatement('DELETE FROM Buying WHERE transaction_id = ?', parameters); + }; + + this.deleteTransaction = function (orderId) { + var parameters = [orderId]; + return DB.queryStatement('DELETE FROM Transactions WHERE id = ?', parameters); + }; + + return this; +}); + +MIM.factory('Sale', function ($cordovaSQLite, DB) { + this.addTransaction = function (saleData) { + var parameters = [ + 'P', + saleData.total_price, + '1', + saleData.customers, + ]; + return DB.queryStatement('INSERT INTO Transactions (categories, total_price, ' + + 'status, customer_id) VALUES (?, ?, ?, ?)', parameters); + }; + + this.addBuying = function (transactionId, saleData) { + var parameters = [ + transactionId, + saleData.products, + saleData.amount, + ]; + return DB.queryStatement('INSERT INTO Buying (transaction_id, product_id, ' + + 'amount) VALUES (?, ?, ?)', parameters); + }; + + return this; +}); + +MIM.factory('Statistic', function ($cordovaSQLite, DB) { + this.getTransaction = function () { + return DB.queryStatement('SELECT strftime(\'%m-%Y\', date) AS month_year, ' + + 'COUNT(id) AS total_transaction FROM Transactions GROUP BY month_year') + .then(function (res) { + return DB.getAll(res); + }); + }; + + this.getIncome = function () { + return DB.queryStatement('SELECT strftime(\'%m-%Y\', date) AS month_year, ' + + 'SUM (total_price) AS total_price FROM Transactions GROUP BY month_year') + .then(function (res) { + return DB.getAll(res); + }); + }; + return this; }); diff --git a/www/templates/inventory_items.html b/www/templates/inventory_items.html index 05337c8..aeffefd 100644 --- a/www/templates/inventory_items.html +++ b/www/templates/inventory_items.html @@ -2,8 +2,8 @@ - {{ item.product_name }} - {{ item.product_amount }} + {{ item.name }} + {{ item.remaining_amount }} diff --git a/www/templates/sales.html b/www/templates/sales.html index 79c2e28..7456b3b 100644 --- a/www/templates/sales.html +++ b/www/templates/sales.html @@ -10,7 +10,7 @@ diff --git a/www/templates/sales_order.html b/www/templates/sales_order.html index f9150a0..95c5803 100644 --- a/www/templates/sales_order.html +++ b/www/templates/sales_order.html @@ -5,7 +5,7 @@ - {{ order.customer_name }} + {{ order.name }} No order :(