<?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: TaxclassController.php, v 1.0 2011/06/14 20:52:11
//
class TaxclassController extends Controller {
    //LISTING TAX CLASSES
    public function indexAction() {
        $ObjTaxclass = new TaxclassModel();
        $this->ObjView->assign("editPrivilege", $this->ObjCommon->hasPrivilege('taxclass/edit/'));
        $deletePrivilege = $this->ObjCommon->hasPrivilege('taxclass/destroy/');
        $this->ObjView->assign("deletePrivilege", $deletePrivilege);
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resListTaxClass = $ObjTaxclass->listTaxClasses($limitFrom, $limitTo, 'ASC');
        $resPagesCount   = $this->setPagesCount($key = 'page');
        $link            = "taxclass/index/";
        if ($this->getRequestValue('page')) {
            $link .= "page/" . $this->getRequestValue('page') . "/";
        }
        if (isset($_POST['taxClassBulkStatus']) && $_POST['taxClassBulkStatus'] != '' && $_POST['taxClassStatus'] != '') {
            if ($_POST['taxClassStatus'] == "Deleted" || $_POST['taxClassStatus'] == "Inactive") {
                if (isset($_POST['delid']) && $_POST['delid'] != '') {
                    $del_array = array_diff($_POST['taxClassBulkStatus'], $_POST['delid']);
                } else {
                    $del_array = $_POST['taxClassBulkStatus'];
                }
                if (count($del_array) > 0) {
                    $resChangeTaxClassStatus = $ObjTaxclass->changeTaxClassStatus($_POST['taxClassStatus'], $del_array, $deletePrivilege);
                    if ($_POST['taxClassStatus'] == "Deleted") {
                        $this->setMessage("language.msgTaxClassDeleteNotAssigned", "message_success");
                    } else {
                        $this->setMessage("language.msgTaxClassUpdateNotAssigned", "message_success");
                    }
                } else {
                    if ($_POST['taxClassStatus'] == "Deleted") {
                        $this->setMessage("language.msgTaxClassCouldNotDelete", "message_error");
                    } else {
                        $this->setMessage("language.msgTaxclassupdated", "message_error");
                    }
                }
            } else {
                $resChangeTaxClassStatus = $ObjTaxclass->changeTaxClassStatus($_POST['taxClassStatus'], $_POST['taxClassBulkStatus'], $deletePrivilege);
                $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
            }
            $this->setRedirect();
        }
        $this->ObjView->assign('rsTaxClasses', $resListTaxClass);
        $this->ObjView->display('TaxClassList.tpl');
    }
    //ADD NEW TAX CLASS
    public function newAction() {
        $ObjTaxclass    = new TaxclassModel();
        $ObjGeozone     = new GeoZoneModel();
        $resListGeozone = $ObjGeozone->listGeoZones('', '', 'Active');
        $this->ObjView->assign('rsGeozone', $resListGeozone);
        if ($this->ObjForm->isValid('formTaxClass', 'submitTaxClass')) {
            $rsSubmitTaxClass = $ObjTaxclass->saveTaxClassDetails($_POST, '');
            $this->setMessage("language.msgTaxClassAdd", "message_success");
            $this->setRedirect("taxclass/");
        }
        $this->ObjView->display('TaxClassAdd.tpl');
    }
    //ADD NEW TAX RATE
    public function ajaxLoadTaxRateTplAction() {
        $taxRateCount   = $_POST['ajax_request'][0];
        $ObjGeozone     = new GeoZoneModel();
        $resListGeozone = $ObjGeozone->listGeoZones('', '', 'Active');
        $this->ObjView->assign('taxRateCount', $taxRateCount);
        $this->ObjView->assign('rsGeozone', $resListGeozone);
        $this->ObjView->display('TaxClassRateAddAjax.tpl');
    }
    //DELETE TAX CLASS
    public function destroyAction() {
        $taxClassID[]       = $this->getRequestValue("id");
        $taxClassID1        = $this->getRequestValue("id");
        $deleteprivilage    = $this->ObjCommon->hasPrivilege('taxclass/destroy/');
        $ObjTaxclass        = new TaxclassModel();
        $resTaxClass        = $ObjTaxclass->getTaxClassDetails($taxClassID1);
        $temp_taxClassCount = $resTaxClass[0]['taxClassCount'];
        $previousURL        = $this->getPreviousURL();
        $this->ObjView->assign('previousURL', $previousURL);
        if ($temp_taxClassCount > 0) {
            $this->setMessage("language.msgTaxClassCouldNotDelete", "message_error");
        } else {
            $resChangeTaxClassStatus = $ObjTaxclass->changeTaxClassStatus('Deleted', $taxClassID, $deleteprivilage);
            $this->setMessage("language.msgTaxClassDelete", "message_success");
        }
        $this->setRedirect($previousURL);
    }
    //EDIT TAX CLASS
    public function editAction() {
        $taxClassID = $this->getRequestValue("id");
        $this->ObjView->assign('taxClassID', $taxClassID);
        $ObjTaxclass = new TaxclassModel();
        $resTaxClass = $ObjTaxclass->getTaxClassDetails($taxClassID);
        if (!$resTaxClass) {
            $this->setRedirect("taxclass/");
        }
        $temp_taxClassCount = $resTaxClass[0]['taxClassCount'];
        $this->ObjView->assignValues($resTaxClass[0]);
        $this->ObjView->assign('resTaxClassCount', $resTaxClass[0]['taxClassCount']);
        $this->ObjView->assign('resTaxRates', $resTaxClass);
        $ObjGeozone     = new GeoZoneModel();
        $resListGeozone = $ObjGeozone->listGeoZones('', '', 'Active');
        $this->ObjView->assign('rsGeozone', $resListGeozone);
        $previousURL = $this->getPreviousURL();
        $this->ObjView->assign('previousURL', $previousURL);
        if ($this->ObjForm->isValid('formTaxClass', 'submitTaxClass')) {
            //	print('<pre>'.print_r($_POST,1).'<pre>');exit();
            $resSaveTaxClassDetails = $ObjTaxclass->saveTaxClassDetails($_POST, $taxClassID);
            if ($temp_taxClassCount > 0 && ($_POST['taxClassStatus'] == 'Inactive')) {
                $this->setMessage("language.msgTaxClassNoStatusUpdate", "message_success");
            } else {
                $this->setMessage("language.msgTaxClassUpdate", "message_success");
            }
            $this->setRedirect($previousURL);
        }
        $this->ObjView->display('TaxClassAdd.tpl');
    }
    //DELETE TAX RATE
    public function ajaxdestroyTaxRateAction() {
        $taxClassID  = $_POST['ajax_request'][0];
        $geoZoneID   = $_POST['ajax_request'][1];
        $ObjTaxclass = new TaxclassModel();
        if ($taxClassID != '' && $geoZoneID != '') {
            $resDeleteTaxRate = $ObjTaxclass->deleteTaxRate($taxClassID, $geoZoneID);
        }
    }
    public function ajaxstatusAction() {
        $ObjTaxclass             = new TaxclassModel();
        $_POST['taxClassStatus'] = $_POST['newstatus'];
        $del_array               = array(
            $_POST['id']
        );
        $resTaxClass             = $ObjTaxclass->getTaxClassDetails($_POST['id']);
        $temp_taxClassCount      = $resTaxClass[0]['taxClassCount'];
        if ($temp_taxClassCount > 0 && ($_POST['taxClassStatus'] == 'Inactive')) {
            $this->setMessage("language.msgTaxClassNoStatusUpdate", "message_success");
        } else {
            $resChangeTaxClassStatus = $ObjTaxclass->changeTaxClassStatus($_POST['taxClassStatus'], $del_array);
            $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
            if ($resChangeTaxClassStatus)
                echo 1;
        }
    }
    public function ajaxIsExistTaxClassNameAction() {
        $taxClassName        = $_POST['ajax_request']['0'];
        $taxClassID          = $_POST['ajax_request']['1'];
        $ObjTaxclass         = new TaxclassModel();
        $isExistTaxClassName = $ObjTaxclass->isExistTaxClassName($taxClassName, $taxClassID);
        echo $isExistTaxClassName;
    }
    //==============  CALL BACK FUNCTIONS ==============================		
    public function isExistTaxClassName() {
        $taxClassName        = $_POST['taxClassName'];
        $taxClassID          = $_POST['taxClassID'];
        $ObjTaxclass         = new TaxclassModel();
        $isExistTaxClassName = $ObjTaxclass->isExistTaxClassName($taxClassName, $taxClassID);
        if ($isExistTaxClassName == '1')
            return false;
        else
            return true;
    }
}