<?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: BusinessrequestController.php, v 1.0 2011/06/14 20:52:11
//
class BusinessrequestController extends Controller {
    public function indexAction() {
        $ObjEmailtemplate    = new EmailtemplateModel();
        $languageIDs         = $this->ObjView->languageIDs;
        $ObjBusinessRequest  = new BusinessRequestModel();
        $link                = 'businessrequest/index/';
        $businessReqStatusID = $this->getRequestValue("rsid");
        $column              = $this->getRequestValue('field');
        $order               = $this->getRequestValue('order');
        $hidlink             = '';
        $fromDate            = '';
        $toDate              = '';
        $settingsDateFormat  = $date_format = $this->getSettingValue('site_settings', 'date_format');
        if ($businessReqStatusID) {
            $resChangeBusinessRequestStatus = $ObjBusinessRequest->changeBusinessRequestStatus($businessReqStatusID, $status = "Declined");
            $previousURL                    = $this->getPreviousURL();
            $this->setMessage("language.msgBusinessRequestDecline", 3);
            $resListBusinessRequest = $ObjBusinessRequest->getBusinessRequestDetails($businessReqStatusID, $languageIDs);
            $languageIDs['Current'] = $resListBusinessRequest['businessReqDefaultLanguageID'];
            $emailTempDet           = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'business-request-rejected');
            $ObjMail                = new Mail($mail_settings);
            $ObjMail->IsHTML();
            $toEmailID     = $resListBusinessRequest['businessReqEmail'];
            $fromEmailName = $emailTempDet['emailFromName'];
            $fromEmailId   = $emailTempDet['emailFromEmail'];
            $Subject       = $emailTempDet['emailSubject'];
            $Body          = $emailTempDet['emailHtml'];
            $companyLogo   = $this->getSettingValue('company_information', 'company_logo');
            $siteName      = $this->getSettingValue('site_settings', 'site_name');
            $siteUrl       = $this->getSettingValue('site_settings', 'global_http_url');
            $Body          = str_replace('{$userName}', $resListBusinessRequest['businessReqFirstName'] . " " . $resListBusinessRequest['businessReqLastName'], $Body);
            $Body          = str_replace('{$siteName}', $siteName, $Body);
            $this->ObjView->assign('emailbody', $Body);
            $this->ObjView->assign('companyLogo', $companyLogo);
            $this->ObjView->assign('siteName', $siteName);
            $this->ObjView->assign('siteUrl', $siteUrl);
            $email_body = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
            ;
            $ObjMail->sendMail(array(
                $fromEmailId,
                $fromEmailName
            ), $toEmailID, $Subject, $email_body);
            if ($ObjMail->Send()) {
            }
            $this->setRedirect('businessrequest/index/');
        }
        if (isset($_POST['submitBusinessRequestSeach'])) {
            $dateSearch = $_POST['fromDate'];
            if ($dateSearch) {
                $searchDates = explode(' - ', $dateSearch);
                $fromDate    = $searchDates[0];
                if (isset($searchDates[1]))
                    $toDate = $searchDates[1];
            }
            $link .= (($fromDate != "") ? "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/" : "");
            $link .= (($toDate != "") ? "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/" : "");
            if ($_POST['businessReqStatus']) {
                $link .= 'status/' . addslashes(urlencode(strtolower($_POST['businessReqStatus']))) . '/';
            }
            if (trim($_POST['busSeachName']) != '') {
                $link .= 'keyword/' . $this->encodeUrlString(trim($_POST['busSeachName'])) . '/';
            }
            $this->setRedirect($link);
        }
        $fromDate       = $this->urlDate($this->getRequestValue("fromdate"), 'get', $settingsDateFormat);
        $toDate         = $this->urlDate($this->getRequestValue("todate"), 'get', $settingsDateFormat);
        $assignFromDate = $fromDate;
        $assignToDate   = $toDate;
        if ($fromDate && $toDate == '') {
            $toDate = $fromDate;
        }
        $busSeachName = $this->decodeUrlString($this->getRequestValue('keyword'));
        $this->ObjView->assign('hiddenBusSeachName', $busSeachName);
        $businessReqStatus = $this->getRequestValue("status");
        $businessReqStatus = ucfirst($businessReqStatus);
        $this->ObjView->assign('hiddenBusinessReqStatus', $businessReqStatus);
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resListBusinessRequest = $ObjBusinessRequest->listBusinessRequests($this->convertDateForSQL($fromDate, $settingsDateFormat), $this->convertDateForSQL($toDate, $settingsDateFormat), $busSeachName, $businessReqStatus, $limitFrom, $limitTo, $column, $order);
        $resPagesCount          = $this->setPagesCount($key = 'page');
        $this->ObjView->assign('businessReqStatus', $businessReqStatus);
        $this->ObjView->assign('date_format', $date_format);
        $this->ObjView->assign('resListBusinessRequest', $resListBusinessRequest);
        // $hidlink .= (($busSeachName != "") ? "keyword/" . $busSeachName . "/" : "");$this->getRequestValue('keyword')
        $hidlink .= (($busSeachName != "") ? "keyword/" . $this->encodeUrlString($busSeachName) . "/" : "");
        $hidlink .= (($businessReqStatus != "") ? "status/" . $businessReqStatus . "/" : "");
        $hidlink .= (($assignFromDate != "") ? "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/" : "");
        $hidlink .= (($assignToDate != "") ? "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/" : "");
        $hidlink .= (($this->getRequestValue("page") != "") ? "page/" . $this->getRequestValue("page") . "/" : "");
        /*assigning from and to values for search*/
        if ($assignFromDate && $assignToDate == '') {
            $this->ObjView->assign('fromDate', $assignFromDate);
        } else if ($assignFromDate && $assignToDate) {
            $this->ObjView->assign('fromDate', $assignFromDate . ' - ' . $assignToDate);
        } else {
            $this->ObjView->assign('fromDate', '');
        }
        /*assigning from and to values for search*/
        $this->ObjView->assign('hidlink', $hidlink);
        $this->ObjView->assign("column", $column);
        $this->ObjView->assign("order", $order == 'ds' ? 'desc' : 'asc');
        $this->ObjView->assign("approvePrivilege", $this->ObjCommon->hasPrivilege('business/approvemerchant/'));
        $this->ObjView->assign("rejectPrivilege", $this->ObjCommon->hasPrivilege('businessrequest/declinebusinessrequest/'));
        $this->ObjView->assign("showPrivilege", $this->ObjCommon->hasPrivilege('businessrequest/show/'));
        $this->ObjView->display('BusinessRequestList.tpl');
    }
    //FUNCTION BUSINESS REQUEST
    public function showAction() {
        $ObjBusinessRequest     = new BusinessRequestModel();
        $ObjBusinessType        = new BusinessTypeModel();
        $businessReqID          = $this->getRequestValue("id");
        $languageIDs            = $this->ObjView->languageIDs;
        $resListBusinessRequest = $ObjBusinessRequest->getBusinessRequestDetails($businessReqID, $languageIDs);
        if (!$resListBusinessRequest) {
            $this->setRedirect('/businessrequest/index/');
        }
        $previousURL    = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
        $siteUrl        = rtrim($this->getSettingValue('site_settings', 'global_http_url'), '/') . '/manage';
        $previousURL    = str_replace($siteUrl, '', $previousURL);
        $businessTypeID = $resListBusinessRequest['businessTypeID'];
        if ($businessTypeID) {
            $rsParentBusinessType                         = $ObjBusinessType->getParentBySubID($businessTypeID, 'Active');
            $resListBusinessRequest['businessTypeParent'] = $rsParentBusinessType['businessTypeName'];
        }
        $website         = $resListBusinessRequest['businessReqWebsite'];
        $website         = str_replace("HTTP://", "http://", $website);
        $website         = str_replace("HTTPS://", "https://", $website);
        $websiteStr      = strstr($website, 'http://');
        $websiteStrHttps = strstr($website, 'https://');
        if ($websiteStr == "" && $websiteStrHttps == "") {
            $website = "http://" . $website;
        }
        $resListBusinessRequest['website'] = $website;
        $this->ObjView->assign('resListBusinessRequest', $resListBusinessRequest);
        $this->ObjView->assign('previousURL', $previousURL);
        $this->ObjView->assign('businessReqLatitude', $resListBusinessRequest['businessReqLatitude']);
        $this->ObjView->assign('businessReqLongitude', $resListBusinessRequest['businessReqLongitude']);
        //sales rep end
        $this->ObjView->display('BusinessRequestShow.tpl');
    }
    // sales rep  
    public function declineBusinessRequestAction() {
        $ObjBusinessRequest             = new BusinessRequestModel();
        $ObjEmailtemplate               = new EmailtemplateModel();
        $languageIDs                    = $this->ObjView->languageIDs;
        $rsid                           = $this->getRequestValue("rsid");
        $mail_settings                  = $this->getSettingValue('mail_settings');
        $mail_settings['smtp_password'] = $this->encryptDecrypt("decrypt", $mail_settings['smtp_password']);
        $resListBusinessRequest         = $ObjBusinessRequest->getBusinessRequestDetails($rsid, $languageIDs);
        if (empty($resListBusinessRequest) || $resListBusinessRequest['businessReqStatus'] != "New") {
            echo '<script type="text/javascript">parent.$.fancybox.close();</script>';
            exit;
        }
        if ($this->ObjForm->isValid('formBusinessRequestDecline', 'submitDecline')) {
            $companyLogo                    = $this->getSettingValue('company_information', 'company_logo');
            $siteName                       = $this->getSettingValue('site_settings', 'site_name');
            $siteUrl                        = $this->getSettingValue('site_settings', 'global_http_url');
            $businessRequestDeclineReason   = $_POST['businessRequestDecline'];
            $resChangeBusinessRequestStatus = $ObjBusinessRequest->changeBusinessRequestStatus($rsid, $status = "Declined");
            //To Business
            if ($rsid > 0) {
                $languageIDs['Current'] = $resListBusinessRequest['businessReqDefaultLanguageID'];
                $emailTempDet           = $ObjEmailtemplate->getEmailTemlateDetails($languageIDs, '', 'business-request-rejected');
                $ObjMail                = new Mail($mail_settings);
                $ObjMail->IsHTML();
                $toEmail       = $resListBusinessRequest['businessReqEmail'];
                $fromEmailName = $emailTempDet['emailFromName'];
                $fromEmailId   = $emailTempDet['emailFromEmail'];
                $Subject       = $emailTempDet['emailSubject'];
                $Body          = $emailTempDet['emailHtml'];
                $Body          = str_replace('{$userName}', $resListBusinessRequest['businessReqFirstName'] . " " . $resListBusinessRequest['businessReqLastName'], $Body);
                $Body          = str_replace('{$businessRequestDeclineReason}', $businessRequestDeclineReason, $Body);
                $Body          = str_replace('{$siteName}', $siteName, $Body);
                $this->ObjView->assign('emailbody', $Body);
                $this->ObjView->assign('companyLogo', $companyLogo);
                $this->ObjView->assign('siteName', $siteName);
                $this->ObjView->assign('siteUrl', $siteUrl);
                $email_body = $this->ObjCommon->fetch("email_templates/Layout.tpl", $languageIDs);
                ;
                $ObjMail->sendMail(array(
                    $fromEmailId,
                    $fromEmailName
                ), $toEmail, $Subject, $email_body);
                if ($ObjMail->Send()) {
                }
            }
            echo '<script type="text/javascript">parent.location.href="' . WEB_BASE_DIR . 'manage/businessrequest/";parent.$.fancybox.close();</script>';
            $this->setMessage("language.msgBusinessRequestDecline", 3);
            exit();
        }
        $this->ObjView->display('DeclineBusinessRequest.tpl');
    }
    //end sales rep
    //END OF FUNCTION
}
