<?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: TaxclassModel.php, v 1.0 2013/03/18 20:52:11
//All functions - user add, edit, list subscribe etc.
//
class TaxclassModel extends Model {
    function getTaxClassDetails($taxClassID) {
        $sql = "
				SELECT 
					taxCla.*, 
					taxRat.taxRateID, 
					taxRat.geoZoneID, 
					taxRat.taxRate, 
					taxRat.taxPriority, 
					taxRat.taxDescription, 
					geo.geoZoneName, 
					geo.geoZoneID, 
					COUNT(
						product.productID
					) AS taxClassCount 
				FROM 
						" . DB_PREFIX . "tax_classes AS taxCla
					LEFT JOIN 
						" . DB_PREFIX . "tax_rates AS taxRat 
							ON taxRat.taxClassID = '" . (int) $taxClassID . "' 
					LEFT JOIN 
						" . DB_PREFIX . "products AS product 
							ON (
								product.taxClassID = taxCla.taxClassID 
								AND product.productStatus = 'Active'
							) 
					LEFT JOIN 
						" . DB_PREFIX . "geozones AS geo ON (
						taxRat.geoZoneID = geo.geoZoneID 
						AND 
						geo.geoZoneStatus = 'Active') 
				WHERE 
					taxCla.taxClassID = '" . (int) $taxClassID . "' 
				GROUP BY 
					taxRat.taxRateID";
        return $this->ObjDatabase->readValues($sql, MYSQLI_BOTH);
    }
    function saveTaxClassDetails($POST, $taxClassID) {
        $taxClassName        = $this->escape($POST['taxClassName']);
        $taxClassDescription = $this->escape($POST['taxClassDescription']);
        $taxClassStatus      = $POST['taxClassStatus'];
        $taxClassCount       = $POST['rsTaxClassCount'];
        $taxRateGroup        = $POST['taxRateGroup'];
        if ($taxClassID > 0) // Update tax class 
            {
            if ($taxClassCount > 0) {
                $taxClassStatus = 'Active';
            }
            $sql = "
				UPDATE 
					" . DB_PREFIX . "tax_classes 
				SET 
					taxClassName='" . $taxClassName . "',
					taxClassDescription='" . $taxClassDescription . "',
					taxClassStatus='" . $taxClassStatus . "' 
				WHERE 
					taxClassID='" . $taxClassID . "'";
            $this->ObjDatabase->setQuery($sql);
        } // Insert tax class 
        else {
            $sql = "
				INSERT INTO " . DB_PREFIX . "tax_classes 
					(
					`taxClassName`,
					`taxClassDescription`,
					`taxClassStatus`
					)  
				VALUES 
					(
					'$taxClassName',
					'$taxClassDescription',
					'$taxClassStatus'
					)";
            $this->ObjDatabase->setQuery($sql);
            $taxClassID = $this->ObjDatabase->getInsertId();
        }
        if (is_array($taxRateGroup)) // Update tax rates
            {
            foreach ($taxRateGroup as $rowHash => $row) // Traverse through all posted tax rates
                {
                $taxRateID = (int) $row['taxrate_id'];
                if ($row['is_delete'] == 1 && $row['taxrate_id'] > -1) // Tax rate is deleted, which is already present in db. Delete it
                    {
                    $sql = " 
						DELETE FROM 
							" . DB_PREFIX . "tax_rates 
						WHERE 
							taxRateID='" . $taxRateID . "'";
                    $this->ObjDatabase->setQuery($sql);
                } elseif ($row['is_delete'] == 0) {
                    $geoZoneID      = (int) $row['geoZoneName'];
                    $taxDescription = $this->escape($row['taxDescription']);
                    $taxRate        = $row['taxRate'];
                    if ($row['taxrate_id'] > -1) // Tax rate already present in db. Update it
                        {
                        $sql = "
							UPDATE 
								" . DB_PREFIX . "tax_rates 
							SET	
								geoZoneID='$geoZoneID',
								taxDescription='$taxDescription',
								taxRate='$taxRate' 
							WHERE 
								taxRateID='" . $taxRateID . "'";
                        $this->ObjDatabase->setQuery($sql);
                    } else // New Tax rate. Insert it
                        {
                        $sql = "
							INSERT INTO " . DB_PREFIX . "tax_rates 
								(
								`taxClassID`,
								`geoZoneID`,
								`taxRate`,
								`taxDescription`
								)  
							VALUES 
								(
								'$taxClassID',
								'$geoZoneID',
								'$taxRate',
								'$taxDescription'
								)";
                        $this->ObjDatabase->setQuery($sql);
                    }
                }
            }
        }
    }
    function listTaxClasses($limitFrom = 0, $limitTo = "", $sortOrder = "ASC", $status = "", $list = "") {
        $sql = "SELECT ";
        //IF PAGINATION REQUIRED						 
        if ($limitTo) {
            $sql .= " 
					SQL_CALC_FOUND_ROWS ";
        }
        if ($list = '') {
            $sql .= " COUNT(product.productID) AS productCount ,";
        }
        $sql .= "taxCla.* 
			FROM  
				" . DB_PREFIX . "tax_classes AS taxCla";
        if ($list = '') {
            $sql .= "			LEFT JOIN 
						" . DB_PREFIX . "products  AS product
							ON	(
								taxCla.taxClassID=product.taxClassID   
								AND 
								product.productStatus!='Deleted'
							) ";
        }
        $sql .= " 
				WHERE 
					taxCla.taxClassStatus!='Deleted'";
        if ($status) {
            $sql .= " 
					AND 
					taxCla.taxClassStatus='" . $status . "' ";
        }
        $sql .= " 
				ORDER BY 
					taxCla.taxClassName " . $sortOrder . " ";
        //IF PAGINATION REQUIRED	
        if ($limitTo) {
            $sql .= " 
				LIMIT  
					" . $limitFrom . "," . $limitTo . "";
        }
        return $this->ObjDatabase->readValues($sql);
    }
    function changeTaxClassStatus($taxClassStatus, $taxClassBulkStatusArray, $deleteprivilage = '') {
        if ($taxClassStatus == 'Deleted' && $deleteprivilage)
            $taxClassStatus = $taxClassStatus;
        else if ($taxClassStatus != 'Deleted')
            $taxClassStatus = $taxClassStatus;
        else
            $taxClassStatus = 'Active';
        $taxClassBulkStatus = implode(',', $taxClassBulkStatusArray);
        if ($taxClassStatus != 'Active') {
            $sql                = "SELECT taxClassID FROM " . DB_PREFIX . "products  
                                                            WHERE (
                                                                    taxClassID IN(" . $taxClassBulkStatus . ") 
                                                                    AND productStatus = 'Active'
                                                            )";
            $resExists          = $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
            $result             = (@array_map(array(
                $this,
                'filterIDs'
            ), $resExists));
            $result             = array_diff($taxClassBulkStatusArray, $result);
            $taxClassBulkStatus = implode(',', $result);
        }
        $sql = "
			UPDATE 
				" . DB_PREFIX . "tax_classes  
			SET 
				taxClassStatus ='$taxClassStatus' 
			WHERE 
				taxClassID IN(" . $taxClassBulkStatus . ")";
        $this->ObjDatabase->setQuery($sql);
    }
    //FUNCTION TO FILTER CITY
    public function filterIDs($resSelectID) {
        return $resSelectID['taxClassID'];
    }
    function deleteTaxRate($taxClassID, $geoZoneID) {
        $sql = "
			DELETE FROM 
				" . DB_PREFIX . "tax_rates 
			WHERE 
				geoZoneID='" . $geoZoneID . "' 
				AND 
				taxClassID='" . $taxClassID . "'";
        $this->ObjDatabase->setQuery($sql);
    }
    function isExistTaxClassName($taxClassName, $taxClassID) {
        $taxClassName = $this->escape($taxClassName);
        $sql          = " 
			SELECT 
				COUNT(*) AS count 
			FROM 
				" . DB_PREFIX . "tax_classes 
			WHERE 
				taxClassName = '" . $taxClassName . "' 
				AND 
				taxClassID != '" . (int) $taxClassID . "' 
				AND 
				taxClassStatus<>'Deleted'";
        $resExistName = $this->ObjDatabase->readValue($sql);
        if ($resExistName['count'] > 0)
            return '1';
        else
            return '0';
    }
}
