<?php
defined('BASEPATH') or exit('No direct script access allowed');

class Welcome extends CI_Controller
{
  function __construct()
  {
    parent::__construct();
    checkLogin();
  }
  public function index()
  {
    $data['dataValue'] = $this->session;
    $this->initDB();
    loadView('index', $data);
    // $this->myaccounts->backupDatabase();
  }
  public function dbbackup()
  {
    $this->myaccounts->backupDatabase();
  }
  public function initDB()
  {

    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_units` (
            `unit_id` int(11) NOT NULL AUTO_INCREMENT,
            `title` varchar(255) DEFAULT NULL,
            `alias` varchar(255) DEFAULT NULL,
            `description` text DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`unit_id`)
          )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_paymentmodes` (
            `paymentmode_id` int(11) NOT NULL AUTO_INCREMENT,
            `title` varchar(255) DEFAULT NULL,
            `alias` varchar(255) DEFAULT NULL,
            `description` text DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`paymentmode_id`)
          )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_itemcategories` (
            `itemcategory_id` int(11) NOT NULL AUTO_INCREMENT,
            `units_id` int(11) NOT NULL,
            `title` varchar(255) DEFAULT NULL,
            `alias` varchar(255) DEFAULT NULL,
            `description` text DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`itemcategory_id`)
          )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_items` (
            `item_id` int(11) NOT NULL AUTO_INCREMENT,
            `itemcategories_id` int(11) DEFAULT NULL,
            `item_code` varchar(255) DEFAULT NULL,
            `title` varchar(255) DEFAULT NULL,
            `alias` varchar(255) DEFAULT NULL,
            `description` text DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`item_id`)
          )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_stocklocations` (
            `stocklocation_id` int(11) NOT NULL AUTO_INCREMENT,
            `title` varchar(255) DEFAULT NULL,
            `alias` varchar(255) DEFAULT NULL,
            `description` text DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`stocklocation_id`)
          )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_stocks` (
            `stock_id` int(11) NOT NULL AUTO_INCREMENT,
            `stocklocations_id` int(11) DEFAULT NULL,
            `ref_id` int(11) DEFAULT NULL,
            `title` varchar(255) DEFAULT NULL,
            `items_id` int(11) DEFAULT NULL,
            `qty` int(11) DEFAULT NULL,
            `price` double(10,2) DEFAULT NULL,
            `display_order` int(11) DEFAULT NULL,
            `status` int(11) DEFAULT NULL,
            `remarks` text DEFAULT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            PRIMARY KEY (`stock_id`)
          )");

    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_purchases` (
            `purchase_id` INT(11) NOT NULL AUTO_INCREMENT,
            `purchase_ref` VARCHAR(50) NULL,
            `transaction_type` VARCHAR(50) NULL,
            `accounts_id` INT(11) NOT NULL COMMENT 'Vendors Account', -- Adding comment here,
            `purchase_date` DATE NOT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            `remarks` TEXT NOT NULL,
            `status` int(11) DEFAULT NULL,
            `discount` DOUBLE(10, 2) NULL,
            PRIMARY KEY (`purchase_id`)
        )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_purchasedetails` (
            `purchasedetails_id` INT(11) NOT NULL AUTO_INCREMENT,
            `purchases_id` INT(11) NOT NULL,
            `items_id` VARCHAR(50) NOT NULL,
            `qty` DOUBLE(10,2) NOT NULL,
            `rate` DOUBLE(10,2) NOT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            `remarks` TEXT NOT NULL,
            `status` int(11) DEFAULT NULL,
            PRIMARY KEY (`purchasedetails_id`)
        )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_sales` (
            `sales_id` INT(11) NOT NULL AUTO_INCREMENT,
            `transaction_type` VARCHAR(50) NOT NULL,
            `accounts_id` INT(11) NOT NULL COMMENT 'Customer Accounts - Sundry Debitors', -- Adding comment here,
            `sales_date` DATE NOT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            `remarks` TEXT NOT NULL,
            `status` int(11) DEFAULT NULL,
            `discount` DOUBLE(10, 2) NULL,
            PRIMARY KEY (`sales_id`)
        )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_salesdetails` (
            `salesdetails_id` INT(11) NOT NULL AUTO_INCREMENT,
            `sales_id` INT(11) NOT NULL,
            `items_id` VARCHAR(50) NOT NULL,
            `qty` DOUBLE(10,2) NOT NULL,
            `rate` DOUBLE(10,2) NOT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            `remarks` TEXT NOT NULL,
            `status` int(11) DEFAULT NULL,
            PRIMARY KEY (`salesdetails_id`)
        )");
    $this->db->query("CREATE TABLE IF NOT EXISTS `tbl_transactions` (
            `transaction_id` INT(11) NOT NULL AUTO_INCREMENT,
            `transaction_type` VARCHAR(50) NOT NULL,
            `stocks_id` INT(11) NOT NULL,
            `qty` INT(11) NOT NULL,
            `transaction_date` DATE NOT NULL,
            `created_on` DATE NOT NULL,
            `created_by` VARCHAR(50) NOT NULL,
            `remarks` TEXT NOT NULL,
            PRIMARY KEY (`transaction_id`)
        )");
    if (!$this->db->get("tbl_stocklocations")->num_rows() > 0) {
      $this->db->query("truncate table tbl_stocklocations");
      $this->db->query("insert into tbl_stocklocations set title='Main Stock', status=1");
    }
    if (!$this->db->get("tbl_units")->num_rows() > 0) {
      $this->db->query("truncate table tbl_units");
      $this->db->query("insert into tbl_units set title='Pcs', status=1");
    }
    if (!$this->db->get("tbl_paymentmodes")->num_rows() > 0) {
      $this->db->query("truncate table tbl_paymentmodes");
      $this->db->query("insert into tbl_paymentmodes set title='Cash', status=1");
    }

    $query = $this->db->query("SHOW COLUMNS FROM tbl_purchases LIKE 'purchase_ref'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_purchases ADD COLUMN purchase_ref VARCHAR(50)");
    }
    $query = $this->db->query("SHOW COLUMNS FROM tbl_accounts LIKE 'account_image'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_accounts ADD COLUMN account_image text");
    }

    $query = $this->db->query("SHOW COLUMNS FROM tbl_accounts LIKE 'account_number'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_accounts ADD COLUMN account_number VARCHAR(50)");
    }

    $query = $this->db->query("SHOW COLUMNS FROM tbl_accounts LIKE 'account_branch'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_accounts ADD COLUMN account_branch VARCHAR(50)");
    }

    
    $query = $this->db->query("SHOW COLUMNS FROM tbl_branches LIKE 'vat'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_branches ADD COLUMN vat int(11)");
    }
    $query = $this->db->query("SHOW COLUMNS FROM tbl_branches LIKE 'pan'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_branches ADD COLUMN pan VARCHAR(20)");
    }
    $query = $this->db->query("SHOW COLUMNS FROM tbl_items LIKE 'units_id'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_items ADD COLUMN units_id INT(11)");
    }
    
    $query = $this->db->query("SHOW COLUMNS FROM tbl_purchases LIKE 'vatToggle'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_purchases ADD COLUMN vatToggle INT(11)");
    }

    $query = $this->db->query("SHOW COLUMNS FROM tbl_sales LIKE 'vatToggle'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_sales ADD COLUMN vatToggle INT(11)");
    }

    $query = $this->db->query("SHOW COLUMNS FROM tbl_accounts LIKE 'account_bank'");

    // If the column doesn't exist, alter the table to add it
    if ($query->num_rows() == 0) {
      $this->db->query("ALTER TABLE tbl_accounts ADD COLUMN account_bank VARCHAR(50)");
    }
  }
}