<?php

    class Dashboard {
        protected $info = array();
        private $db;

        /**
            * The constructor will create an instant of the chosen database adapter (MySQL, MySQLi, etc)
            * and saves it in a private variable $db as an object to be used throughout the class.
            *
            * @author  Taha Sayyad <info@trustotech.com>
            * @uses DBConnectorFactory::loadDriver() to load the database adapter
            */
        public function __construct() {
            $this->db = DBConnectorFactory::loadDriver();
        }

        /**
            * The first thing to do is to set organization data using this method.<br .>
            * Before setting the organization data, the method will remove all unnecessary data from the array.
            * Next it will trim every single value.
            *
            * @author  Taha Sayyad <info@trustotech.com>
            * @param array $data Array of organization data
            */
        public function setdata(array $data) {
            $this->info = $data;
            return $this;
        }
        public function viewSalesInvoicesByDate($date) {
            $data = '';
            $condition = 'AND i.Invoice_Date = CURRENT_DATE';
            if($date='Month')
                $condition.= 'AND i.Invoice_Date = CURRENT_DATE';
            else if($date='Week')
                $condition.= 'AND WEEKOFYEAR(i.Invoice_Date)=WEEKOFYEAR(CURRENT_DATE)';
            $query = $this->db->query('SELECT 
                                            SUM(i.Invoice_Net_Total)
                                        FROM 
                                            tbl_invoices i
                                        WHERE 
                                             1= 1 ' .$condition 
                    );
            while ($row = $this->db->assoc($query)) {
                $data[] = $row;
            }
            return $data;
	   }
        public function viewTopSaledOrPurchasedProducts($table,$limit) {
            $data = '';
            $query = $this->db->query('SELECT 
                                            Product_ID,
                                            Count(Product_ID) as c_p 
                                        FROM 
                                            '.$table.'
                                        GROUP BY 
                                            Product_ID 
                                        ORDER BY 
                                            c_p 
                                        DESC 
                                        LIMIT '.$limit.'
                        ');
            while ($row = $this->db->assoc($query)) {
                $data[] = $row;
            }
            return $data;
        }
        
        public function viewLeastSaledOrPurchasedProducts($table,$limit) {
            $data = '';
            $query = $this->db->query('SELECT 
                                            Product_ID,
                                            Count(Product_ID) as c_p 
                                        FROM 
                                            '.$table.'
                                        GROUP BY 
                                            Product_ID 
                                        ORDER BY 
                                            c_p
                                        LIMIT '.$limit.'
                        ');
            while ($row = $this->db->assoc($query)) {
                $data[] = $row;
            }
            return $data;
        }
        
//         public function viewAccountPayables() {
//            $data = '';
//            $query = $this->db->query('SELECT 
//                                            SUM() 
//                                        FROM 
//                                            tbl_accounting_transactions
//                        ');
//            while ($row = $this->db->assoc($query)) {
//                $data[] = $row;
//            }
//            return $data;
//        }
        
        
    }