<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/
//
//$Id: OrderstatusModel, v 1.0 2017/07/06 11:26:36
//All functions 
//
class OrderstatusModel extends Model{

public function listOrderstatus($params=array()){
        extract($params);
        $selectFields= "";
        if (isset($limitTo) && $limitTo)
                $selectFields .= " SQL_CALC_FOUND_ROWS ";	
        $sql=" 
                SELECT 
                        ".$selectFields."
                        orderstatus.*,
                        ordstaDet.languageID,
                        ordstaDet.orderStatusTitle
                FROM 
                                " . DB_PREFIX . "order_status AS orderstatus
                                  LEFT  JOIN
                                " . DB_PREFIX . "order_status_details AS ordstaDet    
                                    ON orderstatus.orderStatusID=ordstaDet.orderStatusID
                                    AND ordstaDet.languageID='".$languageIDs['Current']."'
                WHERE
                        1
        ";			
                        if(isset($orderStatusTitle) && $orderStatusTitle!=''){
                                $sql.= "
                                        AND
                                        orderStatusTitle like '%".$this->escape($orderStatusTitle)."%'
                                        ";
                        }			
                        if(isset($status) && $status!=''){
                                $sql.= "
                                        AND
                                        status ='".$status."'
                                        ";
                        }else {
                                $sql.= "
                                        AND
                                        status !='D'
                                        ";
                        }	

if(isset($orderby) && isset($sortby) && $sortby!=''){
        $sql.="
                ORDER BY
                        ".substr($sortby, 5)." ".$orderby."
        ";
}
if (isset($limitTo) && $limitTo){
        $sql .= " 
        LIMIT  
                " . $limitFrom . ",
                " . $limitTo . "
        ";
}
return  $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
}

public function getOrderstatus($params=array()){
        extract($params);
        $selectFields= "";
        $sql=" 
                SELECT 
                        ".$selectFields."
                        *
                FROM 
                                " . DB_PREFIX . "order_status AS orderstatus
                                  LEFT  JOIN
                                " . DB_PREFIX . "order_status_details AS ordstaDet    
                                    ON orderstatus.orderStatusID=ordstaDet.orderStatusID
                                    AND ordstaDet.languageID='".$languageIDs['Current']."'
                WHERE
                        orderstatus.orderStatusID = '".$orderStatusID."'
        ";	

        $sql .= " 
        LIMIT  
                1
        ";
        return  $this->ObjDatabase->readValue($sql, MYSQLI_ASSOC);

}

public function changeStatus($params=array()){
        extract($params);
        $sql="
                UPDATE  
                        " . DB_PREFIX . "order_status 
                SET 
                        status = '".$status."' 
                WHERE
                        orderStatusID IN ('".implode("','",$orderStatusIDs)."')
                ";
        $this->ObjDatabase->setQuery($sql);	

}
public function checkKeyExists($tableDtls, $concat = '1') {
        $sql = "SELECT 
							COUNT( 1 )  	
						FROM 
							`" . DB_PREFIX . "" . $tableDtls['tbleName'] . "`
						WHERE 
							" . $tableDtls['fieldKeyNme'] . "='" . $tableDtls['key'] . "'";
        if($tableDtls['ID']>0){
            $sql .= " AND orderStatusID!='".$tableDtls['ID']."' ";
        }
        $result = $this->ObjDatabase->readValueSlave($sql);
        if ($result[0] > 0) {
            $text =  $concat++;
            $tableDtls["key"] = $tableDtls["key"] . '-' . $text;
            $uniqueKey        = $this->checkKeyExists($tableDtls, $concat);
        } else
            $uniqueKey = $tableDtls["key"];
        return $uniqueKey;
    }
public function insert($languageIDs,$orderStatusID=0){
    // ORDER STATUS KEY GENERATION
     if ($_POST['orderStatusTitle'] != '') {
         
                $key = str_replace(" ", "-", trim($this->escape($_POST['orderStatusTitle']))); // Replaces all spaces with hyphens.
                $key = preg_replace('/[^A-Za-z0-9\-]/', '-', $key); // Removes special chars.
                $key = preg_replace('/-+/', '-', $key); // Removes continuous hypens with a single hypen.
                //if($section == "cms")
                $key = trim($key, "-");
                $tableDtls = array(
                        "fieldIdNme" => "orderStatusID",
                        "fieldKeyNme" => "orderStatusKey",
                        "tbleName" => "order_status"
                    );
                $tableDtls["ID"]      = $orderStatusID;
                $tableDtls["key"]      = strtolower($key);
                $key                   = $this->checkKeyExists($tableDtls);
            }
        $notifyCustomer = (isset($_POST['notifyCustomer'])?$this->escape($_POST['notifyCustomer']):'No');
        $isSystem = (isset($_POST['isSystem'])?$this->escape($_POST['isSystem']):'No');
        if($orderStatusID>0){
                $sql="
                        UPDATE 
                                " . DB_PREFIX . "order_status 
                        SET
                                orderStatusState='".$this->escape($_POST['orderStatusState'])."',
                                notifyCustomer='".$notifyCustomer."',     
                                status = '".$this->escape($_POST['status'])."',
                                isSystem='".$isSystem."'       
                        WHERE
                                orderStatusID = '".$orderStatusID."'
                ";
                $this->ObjDatabase->setQuery($sql);
                $currentlanguageID     = $languageIDs['Current'];
                foreach ($languageIDs['AllList']  as $selLanguages) {
                    if ($this->checkStatusLanguageExists($selLanguages, $orderStatusID)) {
                        if ($selLanguages == $currentlanguageID)
                            $sqlUpdate = "UPDATE 
											" . DB_PREFIX . "order_status_details 
										SET 
											orderStatusTitle='".$this->escape($_POST['orderStatusTitle'])."'
											WHERE 
											orderStatusID = " . $orderStatusID . " 
											AND languageID=" . $selLanguages;
                    } else {
                        $sqlUpdate = "INSERT INTO " . DB_PREFIX . "order_status_details (
										`languageID`, `orderStatusTitle`, `orderStatusID`
									   )
									   VALUES(
											" . $selLanguages . ",
											'" . $this->escape($_POST['orderStatusTitle']) . "',
                                                                                        '" . $this->escape($orderStatusID) . "' 
									   )";
                    }
                    if ($sqlUpdate != '')
                        $this->ObjDatabase->setQuery($sqlUpdate);
                }
                
        }
        else{
                $sql="
                        INSERT INTO " . DB_PREFIX . "order_status 
                                (
                                orderStatusTitle,orderStatusKey,status,
                                orderStatusState,
                                notifyCustomer,
                                isSystem
                                )
                        VALUES
                                (
                                '".$key."' ,
                                '".$this->escape($_POST['status'])."',
                                '".$this->escape($_POST['orderStatusState'])."',
                                '".$notifyCustomer."',
                                '".$isSystem."'    )
                        ";
                $this->ObjDatabase->setQuery($sql);	
                $orderStatusID = $this->ObjDatabase->getInsertId();
                $sql = "INSERT INTO `" . DB_PREFIX . "order_status_details`(`languageID`, `orderStatusTitle`, `orderStatusID`) VALUES ";
                foreach($languageIDs['AllList'] as $key =>$language){
                    if ($key > 0)
                    $sql .= " ,";
                    $sql = " ('".$this->escape($language)."','".$this->escape($_POST['orderStatusTitle'])."','".$this->escape($orderStatusID)."')";
                }
                $sql .= ";";
                $this->ObjDatabase->setQuery($sql);
        }
        return $orderStatusID;

}
 //FUNCTION TO CHECK MENU ITEM LANGUAGE EXISTS
    public function checkStatusLanguageExists($languageID, $ID) {
       echo $sql = "SELECT 
				  COUNT(*) 
				FROM 
				 " . DB_PREFIX . "order_status AS orderstatus
                                  LEFT  JOIN
                                " . DB_PREFIX . "order_status_details AS ordstaDet    
                                    ON orderstatus.orderStatusID=ordstaDet.orderStatusID
				WHERE 
				  orderstatus.orderStatusID = '" . $ID . "' 
				  AND status !='D'
				  AND ordstaDet.languageID = " . $languageID;
        $res = $this->ObjDatabase->readValue($sql);
        if ($res[0] > 0) {
            return true;
        } else {
            return false;
        }
    }
public function listOrderstatusInOptions($params=array()){
        extract($params);
        $sql="  SELECT 
                     orderStatusKey,orderStatusTitle  
                FROM 
                                " . DB_PREFIX . "order_status AS orderstatus
                                   LEFT JOIN
                                " . DB_PREFIX . "order_status_details AS ordstaDet    
                                    ON orderstatus.orderStatusID=ordstaDet.orderStatusID
                                    AND ordstaDet.languageID='".$languageIDs['Current']."'
                WHERE
                        1    ";			
                        			
        if(isset($status) && $status!=''){
                $sql.= "AND
                        status ='".$status."'            ";
        }else {
                $sql.= "AND
                        status !='D'";
        }
        if(isset($list) && $list='hash'){
            return  $this->ObjDatabase->getHashSingleArray($sql,array("orderStatusKey","orderStatusTitle") );
        }
return  $this->ObjDatabase->readValues($sql, MYSQLI_NUM);
}
public function getOrderstatusByKey($params=array()){
        extract($params);
        $sql=" 
                SELECT 
                       *
                FROM 
                                " . DB_PREFIX . "order_status AS orderstatus
                                   LEFT JOIN
                                " . DB_PREFIX . "order_status_details AS ordstaDet    
                                    ON orderstatus.orderStatusID=ordstaDet.orderStatusID
                                    AND ordstaDet.languageID='".$languageIDs['Current']."'
                WHERE
                        orderstatus.orderStatusKey = '".$orderStatusKey."'
        ";	

        $sql .= " 
        LIMIT  
                1
        ";
        return  $this->ObjDatabase->readValue($sql, MYSQLI_ASSOC);

}
}
		