<?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: BusinesstypeController.php, v 1.0 2011/06/14 20:52:11
class BusinesstypeController extends Controller {
    public function __construct() {
        $this->init();
    }
    public function indexAction() {
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjBusinessType        = new BusinessTypeModel();
        $link                   = '/businesstype/';
        $sub                    = $this->getRequestValue('type');
        $rsParentBusinessType   = $ObjBusinessType->getParentBusinessType($languageIDs, $busTypeID = '');
        $languageIDs['Current'] = $languageIDs['Content'];
        list($rsBusinessType, $resRecordMaxCount) = $ObjBusinessType->listBusinessTypeLevels($languageIDs, $sub, "", "");
        $businesstypeCount = $ObjBusinessType->getMaxcountForBusinesstype();
        $this->ObjView->assign("businesstypeCount", $businesstypeCount);
        $businesstypeCount = $ObjBusinessType->getMaxcountForBusinesstype();
        $this->ObjView->assign("businesstypeCount", $businesstypeCount);
        $this->ObjView->assign("editprivilege", $this->ObjCommon->hasPrivilege('businesstype/edit/'));
        $this->ObjView->assign("deleteprivilege", $this->ObjCommon->hasPrivilege('businesstype/destroy/'));
        $this->ObjView->assign("resRecordMaxCount", $resRecordMaxCount);
        $this->ObjView->assign("BCount", count($rsParentBusinessType));
        $this->ObjView->assign("rsParentBusinessType", $rsParentBusinessType);
        $this->ObjView->assign("rsBusinessType", $rsBusinessType);
        $this->ObjView->assign("selectedBusinessType", $sub);
        $this->ObjView->assign("page", $this->getRequestValue('page'));
        $this->ObjView->display('BusinessType.tpl');
    }
    //FUNCTION TO ADD NEW BUSINESS TYPE
    public function newAction() {
        $languageIDs               = $this->ObjView->languageIDs;
        $languageIDs['Current']    = $languageIDs['Content'];
        $ObjBusinessType           = new BusinessTypeModel();
        $resListParentBusinessType = $ObjBusinessType->getParentBusinessType($languageIDs, 0, 1, '', 'Active', $this->ObjView->_languageXML);
        $this->ObjView->assign("businessexistcheck", "");
        $this->ObjView->assign("resListParentBusinessType", $resListParentBusinessType);
        if ($this->ObjForm->isValid('formBusTypeSubmit', 'submitBusType')) {
            $rsBusinessType = $ObjBusinessType->submitBusinessType($languageIDs, $_POST);
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            $this->setMessage("language.msgBusinessTypeUpdate", 3);
            exit;
        }
        $this->ObjView->display('BusinessTypeAdd.tpl');
    }
    //END OF FUNCTION
    //FUNCTION TO EDIT BUSINESS TYPE
    public function editAction() {
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $ObjBusinessType        = new BusinessTypeModel();
        $busTypeID              = $this->getRequestValue("id");
        $this->ObjView->assign("businessTypeID", $busTypeID);
        $businessexistcheck = $ObjBusinessType->isBusinessExits($busTypeID);
        if ($businessexistcheck === true)
            $businessexistcheck = "";
        $resListParentBusinessType = $ObjBusinessType->getParentBusinessType($languageIDs, $busTypeID, 1, '', 'Active', $this->ObjView->_languageXML);
        $this->ObjView->assign("businessexistcheck", $businessexistcheck);
        $this->ObjView->assign("resListParentBusinessType", $resListParentBusinessType);
        if ($this->ObjForm->isValid('formBusTypeSubmit', 'submitBusType')) {
            $typeCheck = $_POST['businessTypeStatus'];
            if ($businessexistcheck != '' && ($_POST['businessTypeStatus'] == 'Inactive')) {
                $_POST['businessTypeStatus'] = 'Active';
            }
            $rsBusinessType = $ObjBusinessType->submitBusinessType($languageIDs, $_POST, $busTypeID);
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            if ($businessexistcheck != '' && ($typeCheck == 'Inactive')) {
                $this->setMessage("language.msgBusinesstypeNoStatusUpdate", "message_success");
            } else {
                $this->setMessage("language.msgBusinessTypeUpdate", 3);
            }
            exit;
        }
        if ($busTypeID) {
            $rsBusinessTypeDetails = $ObjBusinessType->getBusinessTypeByID($languageIDs, $busTypeID);
            $this->ObjView->assignValues($rsBusinessTypeDetails);
        }
        $this->ObjView->display('BusinessTypeAdd.tpl');
    }
    //END OF FUNCTION
    //FUNCTION TO DELETE BUSINESS TYPE
    public function destroyAction() {
        $ObjBusinessType = new BusinessTypeModel();
        $busTypeID       = $_POST["id"];
        $rsBusinessType  = $ObjBusinessType->destroyBusinessType($busTypeID);
        if ($rsBusinessType == "error") {
            $this->setMessage("language.msgBusinessTypeCannotDelete", "error");
        } else {
            $this->setMessage("language.msgBusinessTypeDelete", "success");
        }
        if ($rsBusinessType)
            echo 1;
    }
    //END OF FUNCTION
    //FUNCTION TO CHECK BUSINESS TYPE EXISTS OR NOT
    public function ajaxbusinessTypeExitsAction() {
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $businessTypeName       = urldecode($_POST['businessTypeName']);
        $businessTypeParent     = $_POST['businessTypeParent'];
        $businessTypeID         = $_POST['businessTypeID'];
        $ObjBusinessType        = new BusinessTypeModel();
        $check                  = $ObjBusinessType->isBusinessTypeNameExists($languageIDs, $businessTypeParent, $businessTypeName, $businessTypeID);
        return $check;
    }
    //END OF FUNCTION
    //Function to change businessTypeStatus
    public function ajaxchangebustypestatusAction() {
        $languageIDs['Content'] = isset($languageIDs['Content']) ? $languageIDs['Content'] : '';
        $languageIDs['Current'] = $languageIDs['Content'];
        $languageIDs            = $this->ObjView->languageIDs;
        $ObjBusinessType        = new BusinessTypeModel();
        $_POST['busTypeID']     = $_POST['busTypeID'];
        $_POST['changestatus']  = $_POST['businessTypeStatus'];
        $existBusinessType      = $ObjBusinessType->updateBusinessTypeBulkStatus($_POST, $languageIDs);
        echo "1";
        //$this->setMessage("language.msgBusinessTypeStatusUpdate",3);
    }
    //END OF FUNCTION
    //BUSINESS TYPE AND BUSINESS COUNT
    public function ajaxbustypebuscountAction() {
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $businessTypeID         = $_POST['businessTypeID'];
        $mycheckboxes           = array();
        $mycheckboxes           = isset($_POST['myCheckboxes']) ? $_POST['myCheckboxes'] : '';
        $ObjBusinessType        = new BusinessTypeModel();
        if ($businessTypeID != '') {
            $btypecount = $ObjBusinessType->getBusSubTypeCount($languageIDs, $businessTypeID, $mycheckboxes);
            if ($btypecount > 0) {
                echo 'Yes';
                exit;
            } else {
                echo 'No';
                exit;
            }
        } else if (count($mycheckboxes) > 0) {
            $existBusinessTypeboth = $ObjBusinessType->getBusSubTypeCount($languageIDs, $businessTypeID, $mycheckboxes);
            echo $existBusinessTypeboth;
            /*foreach($existBusinessType as $a)
            {
            echo $a.","; }*/
        }
    }
    //END OF FUNCTION 
    //REASSIGN AND  DESTROY BUSINESS TYPE
    public function ajaxreassigninactivatebustypeAction() {
        $businessTypeID   = $_POST['businessTypeID'];
        $businessTypeID2  = $_POST['businessTypeID2'];
        $busTypeIDar      = $_POST['businessTypeIDar1'];
        $businessTypeIDar = explode(",", $busTypeIDar);
        $ObjBusinessType  = new BusinessTypeModel();
        if ($businessTypeID != '') {
            $res = $ObjBusinessType->reassignInactivateBusType($businessTypeID, $businessTypeID2, $businessTypeIDar);
        } else if (count($businessTypeIDar) > 0) {
            $res = $ObjBusinessType->reassignInactivateBusType($businessTypeID, $businessTypeID2, $businessTypeIDar);
        }
        if ($res) {
            echo 'success';
            exit;
        } else {
            echo 'failed';
            exit;
        }
    }
    //END OF FUNCTION 
}