<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Calls extends CI_Controller {

    function __construct() {

        parent::__construct();
    }

    public function addcall() {
        
        
        try
            {

        $jsonStr = file_get_contents('php://input');
        $jsonObj = json_decode($jsonStr);

        if ($jsonObj && $jsonObj->callerid != "" && $jsonObj->calldate != "" && $jsonObj->cash_closing_host != "") {

            $sqlmoney = "SELECT MONEY FROM CLOSEDCASH WHERE DATEEND IS NULL AND HOST LIKE '". $jsonObj->cash_closing_host ."' LIMIT 1;";
        $arrmoney = $this->db->query($sqlmoney)->result();
        $money="";
        
        $sqlcustomer = "SELECT ID FROM CUSTOMERS WHERE ID LIKE '".$jsonObj->callerid."' OR SEARCHKEY LIKE '".$jsonObj->callerid."' OR PHONE LIKE '".$jsonObj->callerid."' OR PHONE2 LIKE '".$jsonObj->callerid."' LIMIT 1;";
        $arrcust = $this->db->query($sqlcustomer)->result();
        $cust=null;
        
        if($arrcust)
        {
            $cust=$arrcust[0]->ID;
        }
        
        if($arrmoney)
        {
            $money=$arrmoney[0]->MONEY;
            
            $data = array(
                'ID' => uniqid(),
                'CALLERID' => $jsonObj->callerid,
                'DATENEW' => $jsonObj->calldate,
                'LINEID' => '1',
                'MONEY' => $money,
                'CUSTOMER' => $cust,
                'STATUS' => 0,
            );

            $this->db->insert('CALLS', $data);

            $data = array(
                'status' => "1",
                'message' => "Call added.",
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
        else {
            $data = array(
                'status' => "0",
                'message' => "Cash Closing Host not found.",
            );

            header('Content-Type: application/json');
            echo json_encode(array("data" => $data));
            exit;
        }

        } else {
            $data = array(
                'status' => "0",
                'message' => "Parameters missing.",
            );

            header('Content-Type: application/json');
            echo json_encode(array("data" => $data));
            exit;
        }
            }
            catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
         $data= array(
        'status' => "0",
        'message' => $e->getMessage(),
                    );
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
                
        exit;
    }
    }
    
    
    public function orderstatus()
    {
        
        try
            {

        $jsonStr = file_get_contents('php://input');
        $jsonObj = json_decode($jsonStr);

        if ($jsonObj && $jsonObj->callerid != "" && $jsonObj->cash_closing_host != "") {

            $sqlmoney = "SELECT CALLS.ID, CALLS.CALLERID, CALLS.DATENEW, CALLS.LINEID, CASE CALLS.STATUS WHEN 0 THEN 'Order not Taken' WHEN 1 THEN 'Preparing' WHEN 2 THEN 'Ready for Delivery' WHEN 3 THEN 'Picked Up' END AS STATUS, DELIVERYORDERS.PAYMENT, DRIVERS.DRIVERNAME, DRIVERS.CODE AS DRIVERCODE FROM CALLS JOIN CLOSEDCASH ON CLOSEDCASH.MONEY=CALLS.MONEY AND CLOSEDCASH.DATEEND IS NULL AND CLOSEDCASH.HOST LIKE '". $jsonObj->cash_closing_host ."' LEFT JOIN DELIVERYORDERS LEFT JOIN DRIVERS ON DRIVERS.ID=DELIVERYORDERS.DRIVERID ON DELIVERYORDERS.CALLID=CALLS.ID WHERE CALLS.CALLERID  LIKE '". $jsonObj->callerid ."' AND CALLS.STATUS<>0 ORDER BY CALLS.DATENEW DESC;";
        $arrmoney = $this->db->query($sqlmoney)->result();
        $money="";
        
        $sqlcustomer = "SELECT * FROM CUSTOMERS WHERE ID LIKE '".$jsonObj->callerid."' OR SEARCHKEY LIKE '".$jsonObj->callerid."' OR PHONE LIKE '".$jsonObj->callerid."' OR PHONE2 LIKE '".$jsonObj->callerid."' LIMIT 1;";
        $arrcust = $this->db->query($sqlcustomer)->result();
        $cust=null;
        
        if($arrcust)
        {
            $cust=$arrcust[0];
        }
        
        if($arrmoney)
        {
           
            
            $data = array(
                
                'CALL' => $arrmoney,
                'CUSTOMER' => $cust,
            );


            $data = array(
                'status' => "1",
                'data' => $data,
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
        else {
            $data = array(
                'status' => "0",
                'message' => "Call not found.",
            );

            header('Content-Type: application/json');
            echo json_encode(array("data" => $data));
            exit;
        }

        } else {
            $data = array(
                'status' => "0",
                'message' => "Parameters missing.",
            );

            header('Content-Type: application/json');
            echo json_encode(array("data" => $data));
            exit;
        }
            }
            catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
         $data= array(
        'status' => "0",
        'message' => $e->getMessage(),
                    );
    
     header('Content-Type: application/json');
    echo json_encode(array("data"=>$data));
                
        exit;
    }
        
    }
    
    
    
    public function orderlist()
    {
        
        try
            {

        $jsonStr = file_get_contents('php://input');
        $jsonObj = json_decode($jsonStr);

        if ($jsonObj && isset($jsonObj->page) && isset($jsonObj->rows) && isset($jsonObj->cash_closing_host)) {
$page = $jsonObj->page;
$rows = $jsonObj->rows;
    $rowstart=$page * $rows;
    
    if(isset($jsonObj->order_status))
{
    $where=" AND CALLS.STATUS=".$jsonObj->order_status." ";
}
 else {
    $where=" ";
}

            $sqlmoney = "SELECT CALLS.ID, CALLS.CALLERID, CALLS.DATENEW, CALLS.LINEID, CASE CALLS.STATUS WHEN 0 THEN 'Order not Taken' WHEN 1 THEN 'Preparing' WHEN 2 THEN 'Ready for Delivery' WHEN 3 THEN 'Picked Up' END AS STATUS, DELIVERYORDERS.PAYMENT, CUSTOMERS.NAME AS CUSTOMERNAME, CUSTOMERS.ADDRESS AS CUSTOMERADDRESS, DRIVERS.DRIVERNAME, DRIVERS.CODE AS DRIVERCODE FROM CALLS JOIN CLOSEDCASH ON CLOSEDCASH.MONEY=CALLS.MONEY AND CLOSEDCASH.DATEEND IS NULL AND CLOSEDCASH.HOST LIKE '". $jsonObj->cash_closing_host ."' LEFT JOIN DELIVERYORDERS LEFT JOIN DRIVERS ON DRIVERS.ID=DELIVERYORDERS.DRIVERID ON DELIVERYORDERS.CALLID=CALLS.ID LEFT JOIN CUSTOMERS ON CUSTOMERS.ID=CALLS.CUSTOMER WHERE CALLS.STATUS<>0 ". $where ." ORDER BY CALLS.DATENEW DESC LIMIT ". $rowstart.",".$rows.";";
        $arrmoney = $this->db->query($sqlmoney)->result();
        $money="";
       
        
        
        
        if($arrmoney)
        {
           
           

            $data = array(
                'status' => "1",
                'data' => $arrmoney,
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
        else {
            $data = array(
                'status' => "0",
                'message' => "Calls not found.",
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }

        } else {
            $data = array(
                'status' => "0",
                'message' => "Parameters missing.",
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
            }
            catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
         $data= array(
        'status' => "0",
        'message' => $e->getMessage(),
                    );
    
     header('Content-Type: application/json');
    echo json_encode($data);
                
        exit;
    }
        
    }
    
    
    public function list()
    {
        
        try
            {

        $jsonStr = file_get_contents('php://input');
        $jsonObj = json_decode($jsonStr);

        if ($jsonObj && isset($jsonObj->page) && isset($jsonObj->rows) && isset($jsonObj->cash_closing_host)) {
            
$page = $jsonObj->page;
$rows = $jsonObj->rows;
$rowstart=$page * $rows;
if(isset($jsonObj->order_status))
{
    $where=" WHERE CALLS.STATUS=".$jsonObj->order_status." ";
}
 else {
    $where=" ";
}
    $sqlmoney = "SELECT CALLS.ID, CALLS.CALLERID, CALLS.DATENEW, CALLS.LINEID, CASE CALLS.STATUS WHEN 0 THEN 'Order not Taken' WHEN 1 THEN 'Preparing' WHEN 2 THEN 'Ready for Delivery' WHEN 3 THEN 'Picked Up' END AS STATUS, DELIVERYORDERS.PAYMENT, CUSTOMERS.NAME AS CUSTOMERNAME, CUSTOMERS.ADDRESS AS CUSTOMERADDRESS, DRIVERS.DRIVERNAME, DRIVERS.CODE AS DRIVERCODE FROM CALLS JOIN CLOSEDCASH ON CLOSEDCASH.MONEY=CALLS.MONEY AND CLOSEDCASH.DATEEND IS NULL AND CLOSEDCASH.HOST LIKE '". $jsonObj->cash_closing_host ."' LEFT JOIN DELIVERYORDERS LEFT JOIN DRIVERS ON DRIVERS.ID=DELIVERYORDERS.DRIVERID ON DELIVERYORDERS.CALLID=CALLS.ID LEFT JOIN CUSTOMERS ON CUSTOMERS.ID=CALLS.CUSTOMER ". $where ." ORDER BY CALLS.DATENEW DESC LIMIT ". $rowstart.",".$rows.";";
        $arrmoney = $this->db->query($sqlmoney)->result();
        $money="";
       
        
        if($arrmoney)
        {
           
           

            $data = array(
                'status' => "1",
                'data' => $arrmoney,
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
        else {
            $data = array(
                'status' => "0",
                'message' => "Calls not found.",
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }

        } else {
            $data = array(
                'status' => "0",
                'message' => "Parameters missing.",
            );

            header('Content-Type: application/json');
            echo json_encode($data);
            exit;
        }
            }
            catch (Exception $e) {
        // this will not catch DB related errors. But it will include them, because this is more general. 
         $data= array(
        'status' => "0",
        'message' => $e->getMessage(),
                    );
    
     header('Content-Type: application/json');
    echo json_encode($data);
                
        exit;
    }
        
    }
    
    
//    public function list() {
//        $sql = "SELECT * FROM CALLS ORDER BY DATENEW DESC;";
//        $arrCalls = $this->db->query($sql)->result();
//        //add the header here
//        header('Content-Type: application/json');
//        echo json_encode(array("data" => $arrCalls));
//    }

}
