<?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.modellist');
if (!class_exists('VmConfig')) {
    include JPATH_SITE.'/administrator/components/com_virtuemart/helpers/config.php';
}
VmConfig::loadConfig();
if (!class_exists('VirtueMartModelOrders')) {
    include VMPATH_ADMIN.DIRECTORY_SEPARATOR.
    'models'.DIRECTORY_SEPARATOR.'orders.php';
}
if (!class_exists('VmModel')) {
    include VMPATH_ADMIN.DIRECTORY_SEPARATOR.
    'helpers'.DIRECTORY_SEPARATOR.'vmmodel.php';
}
/**
 * MarketplaceModelMporders 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 MarketplaceModelMporders extends JModelList
{
    /**
     * __construct function
     */
    function __construct()
    {
        parent::__construct();
    }
    /**
     * GetOrdersList function
     *
     * @param integer $uid     var
     * @param boolean $noLimit var
     * 
     * @return void
     */
    public function getOrdersList($uid = 0, $noLimit = false)
    {
        $db = JFactory::getDBO();
        $query = $db->getQuery(true);
        $orderobj=new VirtueMartModelOrders();
        $tUserInfos = $orderobj->getTable('userinfos');
        $orderobj->_noLimit =5;
        $this->_noLimit =5;
        $concat = array();
        if (property_exists($tUserInfos, 'company')) {
            $concat[]= 'u.company';
        }
        if (property_exists($tUserInfos, 'first_name')) {
            $concat[]= 'u.first_name';
        }
        if (property_exists($tUserInfos, 'middle_name')) {
            $concat[]= 'u.middle_name';
        }
        if (property_exists($tUserInfos, 'last_name')) {
            $concat[]= 'u.last_name';
        }
        if (!empty($concat)) {
            $concatStr = "CONCAT_WS(' ',".implode(',', $concat).")";
        } else {
            $concatStr = 'o.order_number';
        }
        $query->select('o.*')
            ->select($concatStr. 'AS order_name');
        $select_array = array(
            'u.email','pm.payment_name',
            'u.company','u.city','u.zip',
            'u.phone_1','st.address_type','st.company',
            'st.city','st.zip','u.customer_note'
        );
        $select_as = array(
            'order_email','payment_method',
            'company','city','zip',
            'phone','st_type','st_company',
            'st_city','st_zip','customer_note'
        );
        $query->select($db->quoteName($select_array, $select_as))
            ->from($db->quoteName('#__virtuemart_orders', 'o'))
            ->join(
                'LEFT',
                $db->quoteName('#__virtuemart_order_userinfos', 'u') .
                ' ON (' . $db->quoteName('u.virtuemart_order_id') . 
                ' = ' . $db->quoteName('o.virtuemart_order_id') .
                ' AND ' . $db->quoteName('u.address_type') . 
                ' = ' . $db->quote('BT').')'
            )->join(
                'LEFT',
                $db->quoteName('#__virtuemart_order_userinfos', 'st') .
                ' ON (' . $db->quoteName('st.virtuemart_order_id') . 
                ' = ' . $db->quoteName('o.virtuemart_order_id') .
                ' AND ' . $db->quoteName('st.address_type') . 
                ' = ' . $db->quote('ST').')'
            )->join(
                'LEFT',
                $db->quoteName(
                    '#__virtuemart_paymentmethods_'.VmConfig::$vmlang, 'pm'
                ) .' ON (' . $db->quoteName('pm.virtuemart_paymentmethod_id') . 
                ' = ' . $db->quoteName('o.virtuemart_paymentmethod_id') .')'
            );
            
        if (!empty($uid)) {
            $user = JFactory::getUser();
            $uid = (int)$user->id;
            $query->where(
                $db->quoteName('u.virtuemart_user_id'). ' = '. $uid
            );
        }

        if ($this->getState('filter.search') !== ''
            && $this->getState('filter.search') !== null
        ) {
            $search = '"%' .$this->getState('filter.search'). '%"' ;
            $search = str_replace(' ', '%', $search);
            $searchFields = array();
            $searchFields[] = 'u.first_name';
            $searchFields[] = 'u.middle_name';
            $searchFields[] = 'u.last_name';
            $searchFields[] = 'o.order_number';
            $searchFields[] = 'u.company';
            $searchFields[] = 'u.email';
            $searchFields[] = 'u.phone_1';
            $searchFields[] = 'u.address_1';
            $searchFields[] = 'u.zip';
            $searchFields[] = 'st.company';
            $searchFields[] = 'st.last_name';
            $searchFields[] = 'st.city';
            $searchFields[] = 'st.zip';
            $query->where(
                implode(' LIKE '.$search.' OR ', $searchFields). ' LIKE '.$search
            );
        }
        $order_status_code =$this->getState('list.status');
        if ($order_status_code and $order_status_code!=-1) {
            $query->where(
                $db->quoteName('o.order_status')." = ".
                $db->quote($order_status_code)
            );
        }
        $query->order('o.modified_on DESC');
        $db->setQuery($query);
        try{
            $data = $db->loadObjectList();
        }catch(Exception $e){
            JFactory::getApplication()->enqueueMessage(
                $e->getMessage(), 'error'
            );
            return array();
        }
        
        if ($data) {
            foreach ($data as $k=>$d) {
                $data[$k]->order_name = htmlspecialchars(
                    strip_tags(htmlspecialchars_decode($d->order_name))
                );
            }
        }
        return $data;
    }
}
