<commit_before>var MIM = angular.module('starter.controllers', ['ngCordova']);
var db = null;

MIM.controller('AppController', function($location) {
  $location.path('/app/config');
});

MIM.controller('ConfigController', function($ionicPlatform, $ionicLoading, $location, $ionicHistory) {
  $ionicHistory.nextViewOptions({
    disableAnimate: true,
    disableBack: true
  });
  $ionicPlatform.ready(function() {
    $ionicLoading.show({ template: 'Loading...' });
      if (window.cordova) {
        window.plugins.sqlDB.copy('MIM.db', 0, function() {
          db = window.sqlitePlugin.openDatabase({name: 'MIM.db'});
          $ionicLoading.hide();
          $location.path('/app/customer');
        }, function(error) {
          console.error('There was an error copying the database: ' + error);
          db = window.sqlitePlugin.openDatabase({name: 'MIM.db'});
          $ionicLoading.hide();
          $location.path('/app/customer');
        });
      } else {
        console.log('You are using browser to test and run the app.');
        console.log('WebSQL has been deprecated. - http://dev.w3.org/html5/webdatabase/');
        console.log('Please, use the mobile device or emulator instead of browser.');
      }
  });
});

MIM.controller('SalesController', function($scope, $ionicPlatform, $cordovaSQLite) {
  $scope.customers = [];
  $scope.products = [];
  $ionicPlatform.ready(function() {
    var customerQuery = 'SELECT id, name FROM Customers';
    $cordovaSQLite.execute(db, customerQuery, []).then(function(res) {
      if (res.rows.length) {
        for (var i = 0; i < res.rows.length; i++) {
          $scope.customers.push({id: res.rows.item(i).id, customer_name: res.rows.item(i).name});
        }
      }
    }, function(error) {
      console.error(error);
    });

    var productQuery = 'SELECT id, name FROM Products';
    $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);
    });
  });

  $scope.addSale = function(saleData) {
    $scope.sales = [];
    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.');
      }, function(error) {
      console.error(error);
      });
    });
  };
});

MIM.controller('AddInventoryController', function($scope, $ionicPlatform, $cordovaSQLite) {
  $scope.inventory = [];
  $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.');
    }, function(error) {
      console.error(error);
    });
  };
});

MIM.controller('InventoryItemsController', function($scope, $ionicPlatform, $cordovaSQLite) {
    $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) {<commit_msg>update remaining amount of product when the product is bought<commit_after>var MIM = angular.module('starter.controllers', ['ngCordova']);
var db = null;

MIM.controller('AppController', function($location) {
  $location.path('/app/config');
});

MIM.controller('ConfigController', function($ionicPlatform, $ionicLoading, $location, $ionicHistory) {
  $ionicHistory.nextViewOptions({
    disableAnimate: true,
    disableBack: true
  });
  $ionicPlatform.ready(function() {
    $ionicLoading.show({ template: 'Loading...' });
      if (window.cordova) {
        window.plugins.sqlDB.copy('MIM.db', 0, function() {
          db = window.sqlitePlugin.openDatabase({name: 'MIM.db'});
          $ionicLoading.hide();
          $location.path('/app/customer');
        }, function(error) {
          console.error('There was an error copying the database: ' + error);
          db = window.sqlitePlugin.openDatabase({name: 'MIM.db'});
          $ionicLoading.hide();
          $location.path('/app/customer');
        });
      } else {
        console.log('You are using browser to test and run the app.');
        console.log('WebSQL has been deprecated. - http://dev.w3.org/html5/webdatabase/');
        console.log('Please, use the mobile device or emulator instead of browser.');
      }
  });
});

MIM.controller('SalesController', function($scope, $ionicPlatform, $cordovaSQLite) {
  $scope.customers = [];
  $scope.products = [];
  $ionicPlatform.ready(function() {
    var customerQuery = 'SELECT id, name FROM Customers';
    $cordovaSQLite.execute(db, customerQuery, []).then(function(res) {
      if (res.rows.length) {
        for (var i = 0; i < res.rows.length; i++) {
          $scope.customers.push({id: res.rows.item(i).id, customer_name: res.rows.item(i).name});
        }
      }
    }, function(error) {
      console.error(error);
    });

    var productQuery = 'SELECT id, name FROM Products';
    $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);
    });
  });

  $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);
      }, function(error) {
      console.error(error);
      });
    });
  };

  $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.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);
    })
  };
});

MIM.controller('AddInventoryController', function($scope, $ionicPlatform, $cordovaSQLite) {
  $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.');
    }, function(error) {
      console.error(error);
    });
  };
});

MIM.controller('InventoryItemsController', function($scope, $ionicPlatform, $cordovaSQLite) {
    $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) {
{'fraction_non_alphanumeric': 0.16322538578569876, 'fraction_numerical': 0.0016300804173005868, 'mean_word_length': 3.394937917860554, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6849184', 'n_tokens_mistral': 2892, 'n_tokens_neox': 2830, 'n_words': 545}