<?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: RmaController.php, v 1.0 2013/07/30 15:59:59
//
class RmaController extends Controller {
    //INDEX ACTION
    public function indexAction() {
        $ObjRma                 = new RmaModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $settingsDateFormat     = $this->getSettingValue('site_settings', 'date_format');
        $link                   = 'rma/index/';
        $sortByOption           = $this->getRequestValue("sortby");
        $sortByOrder            = $this->getRequestValue("orderby");
        $businessID             = $this->ObjSession->getSession("BUSINESS_USER_ID");
        //$searchToDate			=	isset($searchToDate)?$searchToDate:'';
        if (isset($_POST['submitSearch'])) {
            $searchReturnID      = $this->encodeUrlString(trim($_POST['returnID']));
            $searchMasterOrderID = $this->encodeUrlString(trim($_POST['masterOrderID']));
            $searchFullName      = $this->encodeUrlString(trim($_POST['fullName']));
            if (isset($_POST['searchReturnDate']) && $_POST['searchReturnDate'] != '') {
                if (strpos($_POST['searchReturnDate'], " - ")) {
                    list($fromDate, $toDate) = explode(" - ", $_POST['searchReturnDate']);
                } else {
                    $fromDate = $_POST['searchReturnDate'];
                }
                $searchReturnFromDate = isset($fromDate) ? trim($fromDate) : '';
                $returnToDate         = isset($toDate) ? trim($toDate) : '';
            }
            $searchRmaAction = $_POST['searchRmaAction'];
            $searchRmaStatus = $_POST['searchRmaStatus'];
            if ($searchReturnID != '') {
                $link .= "returnid/" . $searchReturnID . "/";
            }
            if ($searchMasterOrderID != '') {
                $link .= "orderid/" . $searchMasterOrderID . "/";
            }
            if ($searchFullName != '') {
                $link .= "user/" . $searchFullName . "/";
            }
            if (isset($searchReturnFromDate)) {
                $link .= "from/" . $this->urlDate($searchReturnFromDate, 'set', $settingsDateFormat) . "/";
            }
            if (isset($returnToDate) && $returnToDate != '') {
                $link .= "to/" . $this->urlDate($returnToDate, 'set', $settingsDateFormat) . "/";
            }
            if ($searchRmaAction != '') {
                $link .= "returnaction/" . $searchRmaAction . "/";
            }
            if ($searchRmaStatus != '') {
                $link .= "returnstatus/" . $searchRmaStatus . "/";
            }
            $this->setRedirect($link);
        } else {
            $searchReturnID       = $this->decodeUrlString($this->getRequestValue("returnid"));
            $searchMasterOrderID  = $this->decodeUrlString($this->getRequestValue("orderid"));
            $searchFullName       = $this->decodeUrlString($this->getRequestValue("user"));
            $searchReturnFromDate = $this->urlDate($this->getRequestValue("from"), 'get', $settingsDateFormat);
            $returnToDate         = $this->urlDate($this->getRequestValue("to"), 'get', $settingsDateFormat);
            $searchRmaAction      = $this->getRequestValue("returnaction");
            $searchRmaStatus      = $this->getRequestValue("returnstatus");
            if ($searchReturnID != '') {
                $link .= "returnid/" . $this->encodeUrlString($searchReturnID) . "/";
            }
            if ($searchMasterOrderID != '') {
                $link .= "orderid/" . $this->encodeUrlString($searchMasterOrderID) . "/";
            }
            if ($searchFullName != '') {
                $link .= "user/" . $this->encodeUrlString($searchFullName) . "/";
            }
            if ($searchReturnFromDate) {
                $link .= "from/" . $this->urlDate($searchReturnFromDate, 'set', $settingsDateFormat) . "/";
            }
            if ($returnToDate) {
                $link .= "to/" . $this->urlDate($returnToDate, 'set', $settingsDateFormat) . "/";
            }
            if ($searchRmaAction != '') {
                $link .= "returnaction/" . $searchRmaAction . "/";
            }
            if ($searchRmaStatus != '') {
                $link .= "returnstatus/" . $searchRmaStatus . "/";
            }
        }
        if ((trim($searchReturnID) > 0 && ctype_digit($searchReturnID)) || (trim($searchReturnID == ''))) {
            list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
            $params['limitFrom']           = $limitFrom;
            $params['limitTo']             = $limitTo;
            $params['languageIDs']         = $languageIDs;
            $params['searchReturnID']      = $searchReturnID;
            $params['searchMasterOrderID'] = $searchMasterOrderID;
            $params['searchFullName']      = $searchFullName;
            $params['fromDate']            = $this->convertDateForSQL($searchReturnFromDate, $settingsDateFormat);
            $params['toDate']              = $this->convertDateForSQL($returnToDate, $settingsDateFormat);
            $params['searchRmaAction']     = $searchRmaAction;
            $params['searchRmaStatus']     = $searchRmaStatus;
            $params['sortByOption']        = $sortByOption;
            $params['sortByOrder']         = $sortByOrder;
            $params['businessID']          = $businessID;
            $resReturnRequest              = $ObjRma->listReturnRequest($params);
        }
        $resPagesCount = $this->setPagesCount($key = 'page');
        $resRmaAction  = $ObjRma->listAllProperties(0, '', $languageIDs, 'A', '', '', '', '', '');
        $resRmaStatus  = $ObjRma->listStatusOption($languageIDs, 'R');
        if ($sortByOption == 'returnID' && $sortByOrder == "asc") {
            $sortReturnID = "desc";
        } else {
            $sortReturnID = "asc";
        }
        if ($sortByOption == 'masterOrderID' && $sortByOrder == "asc") {
            $sortMasterOrderID = "desc";
        } else {
            $sortMasterOrderID = "asc";
        }
        if ($sortByOption == 'fullName' && $sortByOrder == "asc") {
            $sortFullName = "desc";
        } else {
            $sortFullName = "asc";
        }
        if ($sortByOption == 'returnDate' && $sortByOrder == "asc") {
            $sortReturnDate = "desc";
        } else {
            $sortReturnDate = "asc";
        }
        if ($sortByOption == '' && $sortByOrder == "") {
            $sortByOption = "returnID";
            $sortReturnID = "asc";
            $sortByOrder  = "desc";
        }
        $this->ObjView->assign('resRmaActions', $resRmaAction);
        $this->ObjView->assign('resRmaStatus', $resRmaStatus);
        $this->ObjView->assign('resReturnRequest', isset($resReturnRequest) ? $resReturnRequest : '');
        $this->ObjView->assign('returnID', $searchReturnID);
        $this->ObjView->assign('masterOrderID', $searchMasterOrderID);
        $this->ObjView->assign('fullName', $searchFullName);
        if ($searchReturnFromDate && $returnToDate == '') {
            $this->ObjView->assign('searchReturnDate', $searchReturnFromDate);
        } else if ($searchReturnFromDate && $returnToDate) {
            $this->ObjView->assign('searchReturnDate', $searchReturnFromDate . ' - ' . $returnToDate);
        } else {
            $this->ObjView->assign('searchReturnDate', '');
        }
        $this->ObjView->assign('searchRmaAction', $searchRmaAction);
        $this->ObjView->assign('searchRmaStatus', $searchRmaStatus);
        $this->ObjView->assign('sortByOrder', $sortByOrder);
        $this->ObjView->assign('sortByOption', $sortByOption);
        $this->ObjView->assign('sortReturnID', $sortReturnID);
        $this->ObjView->assign('sortMasterOrderID', $sortMasterOrderID);
        $this->ObjView->assign('sortFullName', $sortFullName);
        $this->ObjView->assign('sortReturnDate', $sortReturnDate);
        $this->ObjView->assign('link', $link);
        $this->ObjView->display('ReturnRequests.tpl');
    }
    public function returndetailsAction() {
        $ObjRma      = new RmaModel();
        $languageIDs = $this->ObjView->languageIDs;
        $returnID    = (int)$this->getRequestValue('id');
        $history     = $this->getPreviousURL();
        $businessID  = $this->ObjSession->getSession("BUSINESS_USER_ID");
        $currentUrl  = "rma/returndetails/id/" . $returnID . "/";
        if ($history == $currentUrl) {
            $history = "/rma/index/";
        }
        $languageIDs['Current']  = $languageIDs['Default'];
        $resApprovedReturn       = $ObjRma->getReturnProductDetails($languageIDs, '', 'A', '', $returnID);        
        $resDeclinedReturn       = $ObjRma->getReturnProductDetails($languageIDs, '', 'D', '', $returnID);
        if(!empty($resApprovedReturn) && $resApprovedReturn[0]['businessID']!=$businessID) {
            $this->setRedirect("/rma/index/");
        } elseif(empty($resApprovedReturn) && !empty($resDeclinedReturn) && $resDeclinedReturn[0]['businessID']!=$businessID) {
            $this->setRedirect("/rma/index/");
        }        
        $params['languageIDs']   = $languageIDs;
        $params['returnID']      = $returnID;
        $resReturnDetails        = $ObjRma->listReturnRequest($params);
        $resRequestStatusDetails = $ObjRma->listRmaStatues(0, '', $languageIDs, 'R', isset($resReturnDetails['merchantRequestStatusID']) ? $resReturnDetails['merchantRequestStatusID'] : '');
        if (isset($_POST['submitAction'])) {
            $resSaveRequest = $ObjRma->saveMerchantRequest($languageIDs);
            $this->setMessage("language.msgRequestSendSuccessFully", "success");
            $this->setRedirect();
        }
        if (isset($resReturnDetails['statusHistory']) && $resReturnDetails['statusHistory'] != '') {
            $statusHistory = unserialize($resReturnDetails['statusHistory']);
            $this->ObjView->assign("statusHistory", $statusHistory);
        }
        $dateFormat     = $this->getSettingValue("site_settings", "date_format");
        $timeFormat     = $this->getSettingValue('site_settings', 'time_format');
        $resRmaStatuses = $ObjRma->listRmaStatues('', '', $languageIDs, 'R');
        $this->ObjView->assign('resApprovedReturn', $resApprovedReturn);
        $this->ObjView->assign('resDeclinedReturn', $resDeclinedReturn);
        $this->ObjView->assign('resReturnDetails', (isset($resReturnDetails)) ? $resReturnDetails : '');
        $this->ObjView->assign('rmaStatuses', $resRmaStatuses);
        $this->ObjView->assign('returnID', $returnID);
        $this->ObjView->assign('extraData', (isset($resReturnDetails['extra'])) ? unserialize($resReturnDetails['extra']) : '');
        $this->ObjView->assign("date_format", $dateFormat);
        $this->ObjView->assign("time_format", $timeFormat);
        $this->ObjView->assign("history", $history);
        $this->ObjView->assign("resRequestStatus", $resRequestStatusDetails);
        $this->ObjView->display('RmaReturnDetails.tpl');
    }
    public function labelAction() {
        $ObjRma                = new RmaModel();
        $ObjOrder              = new OrderModel();
        $languageIDs           = $this->ObjView->languageIDs;
        //search parameters
        $returnID              = $this->decodeUrlString($this->getRequestValue('id'));
        $resApprovedReturn     = $ObjRma->getReturnProductDetails($languageIDs, '', 'A', '', $returnID);
        $masterOrderID         = $resApprovedReturn[0]['masterOrderID'];
        $orderDetails          = $ObjOrder->getProductOrderDetails($languageIDs, '', '', $masterOrderID);
        $params['languageIDs'] = $languageIDs;
        $params['returnID']    = $returnID;
        $resReturnDetails      = $ObjRma->listReturnRequest($params);
        $billingAddress        = unserialize($orderDetails[0]['billingAddress']);
        $shippingAddress       = unserialize($orderDetails[0]['shippingAddress']);
        $this->ObjView->assign("resProducts", $resApprovedReturn);
        $this->ObjView->assign("resReturnDetails", $resReturnDetails);
        $this->ObjView->assign("orderDetails", $orderDetails);
        $this->ObjView->assign("billingAddress", $billingAddress);
        $this->ObjView->assign("shippingAddress", $shippingAddress);
        $this->ObjView->assign("shippingAddressFormat", array_values($shippingAddress));
        $adminInfo      = $this->getSettingValue("company_information", "address_1"); 
            
        if($this->getSettingValue("company_information", "address_2")!='') {
            $adminInfo  .=    ",<br>".$this->getSettingValue("company_information", "address_2");
        }            
        if($this->getSettingValue("company_information", "company_city")!='') {
            $adminInfo  .=    ",<br>".$this->getSettingValue("company_information", "company_city");
        }
        if($this->getSettingValue("company_information", "company_zip")!='') {
            $adminInfo  .=    ",<br>".$this->getSettingValue("company_information", "company_zip");
        }
        if($this->getSettingValue("company_information", "company_state")!='' && $this->getSettingValue("company_information", "company_city")!='' && $this->getSettingValue("company_information", "company_state")!=$this->getSettingValue("company_information", "company_city")) {
            $adminInfo  .=    ",<br>".$this->getSettingValue("company_information", "company_state");
        }
        if($this->getSettingValue("company_information", "company_country")!='') {
            $adminInfo  .=    ",<br>".$this->getSettingValue("company_information", "company_country");
        }
                
        $this->ObjView->assign("adminInfo", $adminInfo);
        $this->ObjView->assign("companyName", $this->getSettingValue("company_information", "company_name"));
        $this->ObjView->assign("companyPhone", $this->getSettingValue("company_information", "company_phone"));
        $this->ObjView->assign("companyFax", $this->getSettingValue("company_information", "company_fax"));
        $this->ObjView->assign("companyWebsite", $this->getSettingValue("company_information", "company_website"));
        $this->ObjView->assign("companyEmail", $this->getSettingValue("company_information", "company_email"));
        $this->ObjView->display('ProductRmaLabelsList.tpl');
    }
}