<?php
/**
 * Marketplace - Component Marketplace 
 * ******************************************************************
 *
 * PHP version 7.0
 *
 * @category   Component
 * @package    Joomla
 * @author     WebKul software private limited <support@webkul.com>
 * @copyright  2010 WebKul software private limited
 * @license    http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @version    GIT:5.2
 * @filesource http://store.webkul.com
 * @link       Technical Support:  webkul.uvdesk.com
 * ******************************************************************
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
jimport('joomla.html.html');
jimport('joomla.html.behavior');
/**
 * MarketplaceViewMporders class
 * 
 * @category Component
 * @package  Joomla
 * @author   WebKul software private limited <support@webkul.com>
 * @license  http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link     Technical Support:  webkul.uvdesk.com
 */
class MarketplaceViewMporders extends JViewLegacy
{
    protected $items;
    protected $pagination;
    protected $state;
    public static $vmAdminAreaStarted = false;
    public static $backEnd = true;
    /**
     * StartAdminArea function
     *
     * @param [type] $vmView     var
     * @param string $selectText var
     * 
     * @return void
     */
    static function startAdminArea(
        $vmView,$selectText = 'COM_VIRTUEMART_DRDOWN_AVA2ALL'
    ) {
        if (vRequest::getCmd('format') =='pdf') {
            return;
        }
        if (vRequest::getCmd('manage', false)) {
            self::$backEnd=false;
        }
        if (self::$vmAdminAreaStarted) {
            return;
        }
        self::$vmAdminAreaStarted = true;
        $admin = 'administrator/components/com_virtuemart/assets/css';
        $modalJs='';
        vmJsApi::css('admin_ui', $admin);
        vmJsApi::css('toolbar_images', $admin);
        vmJsApi::addJScript(
            '/administrator/components/com_virtuemart/assets/js/jquery.coookie.js'
        );
        vmJsApi::addJScript(
            '/administrator/components/com_virtuemart/assets/js/vm2admin.js'
        );
        $vm2string = "editImage: 'edit image',select_all_text: '".
            vmText::_('COM_VIRTUEMART_DRDOWN_SELALL').
            "',select_some_options_text: '".
            vmText::_($selectText)."'" ;
    ?>
    <?php if (!self::$backEnd ) {
        $bar = JToolbar::getInstance('toolbar');
    ?><div class="toolbar-box" style="height: 84px;position: relative;"><?php
        echo $bar->render()
    ?></div>
    <?php } ?>
    <div id="admin-content" class="admin-content">
    <?php
    }
    /**
     * Display function
     *
     * @param [type] $tpl var
     * 
     * @return void
     */
    function display($tpl = null)
    {
        $this->sidebar = JHtmlSidebar::render();
        $this->items = $this->get('Items');
        $this->pagination = $this->get('Pagination');
        $this->state = $this->get('State');
        $this->filterForm    = $this->get('FilterForm');
        $this->activeFilters = $this->get('ActiveFilters');
        $view = vRequest::getCmd('view', vRequest::getCmd('controller', 'orders'));
        if (!class_exists('VmConfig')) {
            include JPATH_SITE.
            '/administrator/components/com_virtuemart/helpers/config.php';
        }
        if (!class_exists('AdminUIHelper')) {
            include JPATH_SITE.
            '/administrator/components/com_virtuemart/helpers/adminui.php';
        }
        if (!class_exists('CurrencyDisplay')) {
            include VMPATH_ADMIN . DIRECTORY_SEPARATOR . 'helpers' .
            DIRECTORY_SEPARATOR . 'currencydisplay.php';
        }
        if (!class_exists('VmHTML')) {
            include VMPATH_ADMIN . DIRECTORY_SEPARATOR . 'helpers'
            . DIRECTORY_SEPARATOR . 'html.php';
        }
        if (!class_exists('vmPSPlugin')) {
            include VMPATH_PLUGINLIBS.DIRECTORY_SEPARATOR.'vmpsplugin.php';
        }
        if (!class_exists('MarketplaceHelper')) {
            include JPATH_ADMINISTRATOR.DIRECTORY_SEPARATOR.'components'.
            DIRECTORY_SEPARATOR.'com_marketplace'.DIRECTORY_SEPARATOR.'helpers'.
            DIRECTORY_SEPARATOR.'marketplace.php';
        }
        VmConfig::loadConfig();
        $orderStatusModel=VmModel::getModel('orderstatus');
        $orderStates = $orderStatusModel->getOrderStatusList(true);
        $orderModel =VmModel::getModel('orders');
        $curTask = vRequest::getCmd('task');
        $model = $this->getModel('mporders');
        $seller_id=$model->getState('list.seller');
        $orderStatusModel =VmModel::getModel('orderstatus');
        $orderstates = vRequest::getCmd('order_status_code', '');
        $this->lists['state_list'] = $orderStatusModel->renderOSList(
            $orderstates, 'order_status_code',
            false, ' onchange="this.form.submit();" '
        );
        $orderslist = $model->getOrdersList();
        if (isset($seller_id) && strlen($seller_id)) {
            $seller_products=MarketplaceHelper::getSellerProductList($seller_id);
            foreach ($orderslist as $key => $orderlist) {
                $flag=0;
                $items=$orderModel->getOrder($orderlist->virtuemart_order_id);
                foreach ($items['items'] as $key1 =>$item) {
                    if (in_array($item->virtuemart_product_id, $seller_products)) {
                        $flag=1;
                    }
                }
                if ($flag==0) {
                    unset($orderslist[$key]);
                }
            }
            $orderslist=array_values($orderslist);
        }
        $this->assignRef('orderstatuses', $orderStates);
        if (!class_exists('CurrencyDisplay')) {
            include VMPATH_ADMIN.DIRECTORY_SEPARATOR.
            'helpers'.DIRECTORY_SEPARATOR.'currencydisplay.php';
        }
        $_currencies = array();
        if ($orderslist) {
            foreach ($orderslist as $virtuemart_order_id => $order) {
                if (!empty($order->order_currency)) {
                    $currency = $order->order_currency;
                } elseif ($order->virtuemart_vendor_id) {
                    if (!class_exists('VirtueMartModelVendor')) {
                        include VMPATH_ADMIN.DIRECTORY_SEPARATOR.'models'.
                        DIRECTORY_SEPARATOR.'vendor.php';
                    }
                    $currObj = VirtueMartModelVendor::getVendorCurrency(
                        $order->virtuemart_vendor_id
                    );
                    $currency = $currObj->virtuemart_currency_id;
                }
                if (!array_key_exists('curr'.$currency, $_currencies)) {
                    $_currencies['curr'.$currency] = CurrencyDisplay::getInstance(
                        $currency, $order->virtuemart_vendor_id
                    );
                }
                $order->order_total = $_currencies['curr'.$currency]
                    ->priceDisplay($order->order_total);
                $order->invoiceNumber = $orderModel
                    ->getInvoiceNumber($order->virtuemart_order_id);
            }
        }
        JToolBarHelper::spacer('80');
        JToolBarHelper::preferences('com_marketplace');
        JToolBarHelper::title(
            JText::_('COM_MARKETPLACE_ORDERS'), 'marketplaceorders'
        );
        $this->assignRef('orderslist', $orderslist);
        $this->pagination = $model->getPagination();
    
        if (JFactory::getApplication()->isSite()) {
            $bar = JToolBar::getInstance('toolbar');
            $bar->appendButton(
                'Link', 'back', 'COM_VIRTUEMART_LEAVE',
                'index.php?option=com_virtuemart&manage=0'
            );
        }
        shopFunctions::checkSafePath();
        parent::display($tpl);
    }
}
