<?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: CurrencyController.php, v 1.0 2011/06/14 20:52:11
//
class CurrencyController extends Controller {
    // LIST/ADD/EDIT CURRENCIES
    public function indexAction() {
        $ObjCurrency = new CurrencyModel();
        $currencyID  = isset($_POST["currencyDefault"]) ? $_POST["currencyDefault"] : '';
        $redirect    = false;
        if ($currencyID && isset($_POST['setDefaultCurrency'])) {
            $cdetails = $this->ObjView->_currencies[$currencyID];
            $cname    = $cdetails['currencyCode'];
            // If wrong currency code found
            if ($cdetails['currencyType'] == 'Standard') {
                try {
                    $ObjCurrency->updateCurrencyExchRateUsingAPI($currencyID);
                }
                catch (Exception $exc) {
                    $this->setMessage("language.msgCurrencyCodeErrorMessage", "message_error");
                    $this->setRedirect("currency/index/");
                }
                $ObjCurrency->changeDefaultCurrency($currencyID);
            } else {
                $this->setMessage("language.msgCurrencyCodeErrorMessage", "message_error");
                $this->setRedirect("currency/index/");
            }
            $redirect1 = true;
        }
        if (isset($_POST['updateCurrencyRate'])) {
            $currencyID = $this->ObjView->_defaultCurrency;
            try {
                $ObjCurrency->updateCurrencyExchRateUsingAPI($currencyID);
            }
            catch (Exception $exc) {
                $this->setMessage("language.msgExgCurrencyCodeErrorMessage", "message_error");
                $this->setRedirect("currency/index/");
            }
            $redirect = true;
        }
        if ($this->ObjForm->isValid('formStdCurrency', 'updateStdCurrency')) {
            if (!$ObjCurrency->isCurrencyCodeAlreadyExists($_POST['currencyCodeStandard'])) {
                $ObjCurrency->addStandardCurrencyToSystem($_POST);
                $redirect2 = true;
            }
        }
        if ($redirect) {
            $this->setMessage("language.msgCurrencyDetailsSave", "success");
            $this->setRedirect("currency/index/");
        } elseif (isset($redirect1)) {
            $this->setMessage($cname . " set as default currency", "success");
            $this->setRedirect("currency/index/");
        } elseif (isset($redirect2)) {
            $this->setMessage("language.msgCurrencyAdd", "success");
            $this->setRedirect("currency/index/");
        }
        $rsCurrencies     = $ObjCurrency->listCurrencies("currencyName ASC", NULL, NULL, 'Yes');
        $resStdCurrencies = $ObjCurrency->listStandardCurrencies();
        $this->ObjView->assign("currencyDetails", $rsCurrencies);
        $this->ObjView->assign("resStdCurrencies", $resStdCurrencies);
        $this->ObjView->assign("addPrivilege", $this->ObjCommon->hasPrivilege('currency/new/'));
        $this->ObjView->assign("editPrivilege", $this->ObjCommon->hasPrivilege('currency/edit/'));
        $this->ObjView->assign("deletePrivilege", $this->ObjCommon->hasPrivilege('currency/destroy/'));
        $this->ObjView->assign("updateRatePrivilege", $this->ObjCommon->hasPrivilege('currency/updaterate/'));
        $this->ObjView->display('Currency.tpl');
    }
    //DELETE CURRENCY
    public function destroyAction() {
        $ObjCurrency = new CurrencyModel();
        $currencyID  = $this->getRequestValue("id");
        if ($currencyID == $this->ObjView->_defaultCurrency) {
            $this->setMessage("language.msgCurrencyDefaultDelete", "message_error");
            $this->setRedirect("currency/index/");
        } else if ($ObjCurrency->getUpcomingProductCount($currencyID) > 0) {
            $this->setMessage("language.lblProductCurrencyExists", "message_error");
            $this->setRedirect("currency/index/");
        }
        $ObjCurrency->changeCurrencyStatus($currencyID, "Delete");
        $this->setMessage("language.msgCurrencyDelete", 3);
        $this->setRedirect("currency/index/");
    }
    public function newAction() {
        $ObjCurrency = new CurrencyModel();
        if ($this->ObjForm->isValid('formCurrency', 'submitCurrency')) {
            $ObjCurrency->saveCurrencyDetails($_POST);
            $this->setMessage("language.msgCurrencyAdd", "success");
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            exit;
        }
        $this->ObjView->display('CurrencyAdd.tpl');
    }
    public function editAction() {        
        $ObjCurrency    = new CurrencyModel();
        $ObjLanguage    = new LanguageModel();        
        $currencyID     = $this->getRequestValue('id');
        $resCurrencyDet = $this->ObjView->_currencies[$currencyID];
        $languages      = $ObjLanguage->listLanguages();
        if ($this->ObjForm->isValid('formCurrency', 'submitCurrency')) {
            try {
                $ObjCurrency->saveCurrencyDetails($_POST, $currencyID,$languages);
                $this->setMessage("language.msgCurrencyDetailsSave", "success");
            }
            catch (Exception $exc) {
                $this->setMessage("language.msgExgCurrencyCodeErrorMessage", "message_error");
            }
            echo '<script type="text/javascript">window.parent.location = window.parent.location.href;parent.$.colorbox.close(); </script>';
            exit;
        }
        
        $resCurrencyDet['currencySymbol']             = $resCurrencyDet['currencySymbolLeft'] . $resCurrencyDet['currencySymbolRight'];
        $resCurrencyDet['currencySymbolLeftOthers']   = !empty($resCurrencyDet['currencySymbolLeftOthers'])?unserialize($resCurrencyDet['currencySymbolLeftOthers']):'';
        $resCurrencyDet['currencySymbolRightOthers']  = !empty($resCurrencyDet['currencySymbolRightOthers'])?unserialize($resCurrencyDet['currencySymbolRightOthers']):'';
        $resCurrencyDet['currencySymbolPosition'] = (isset($resCurrencyDet['currencySymbolLeft']) && $resCurrencyDet['currencySymbolLeft']) ? 'Left' : 'Right';
        $this->ObjView->assignValues($resCurrencyDet);        
        $this->ObjView->assign('activeLanguages', $languages);       
        $this->ObjView->display('CurrencyAdd.tpl');
    }
}
