<?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');
if (!class_exists('VmConfig')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/config.php';
}
if (!class_exists('ShopFunctions')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/shopfunctions.php';
}
if (!class_exists('CurrencyDisplay')) {
    include JPATH_ADMINISTRATOR.
    '/components/com_virtuemart/helpers/currencydisplay.php';
}
VmConfig::loadConfig();
VmConfig::loadJLang('com_virtuemart');
/**
 * MarketplaceViewAddproduct 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 MarketplaceViewAddproduct extends JViewLegacy
{
    /**
     * This is display function
     *
     * @param [type] $tpl view
     * 
     * @return void
     */
    public function display($tpl = null)
    {
        global $option;
        $db = JFactory::getDBO();
        $userdetails = JFactory::getUser();
        $model=$this->getModel();
        $is_seller=$model->isSeller($userdetails->id);
        
        if ($userdetails->id>0) {
            $this->form=$this->get('form');
            if (isset($is_seller)) {
                $lists = array();
                $data =  $this->get('Data');
                $images =  $this->get('imagepath');
                $catid = '';
                $price_tax = '';
                $discount = '';
                $vm_product_id = '';
                $categoryIds='';
                $javascript = '';
                $discount_id=0;
                $jinput=JFactory::getApplication()->input;
                $method=$jinput->getMethod();
                $this->virtuemart_product_id=$jinput->get(
                    'virtuemart_product_id',
                    0,
                    'INT'
                );
                if (count($data)) {
                    if (!isset($data[0]->product_discount_id)) {
                        $data[0]->product_discount_id= 0;
                    }
                    for ($i=0, $n=count($data); $i < $n; $i++) {
                        $item = &$data[$i];
                        $catid = $item->product_parent_id;
                        $vm_product_id = $item->virtuemart_product_id;
                        $productmodel=VmModel::getModel('product');
                        $vmdata=$productmodel->getProduct(
                            $this->virtuemart_product_id
                        );
                        if (isset($vmdata->allPrices[0]['product_tax_id'])) {
                            $calc_tname=$model->getTaxCalculationName(
                                $vmdata->allPrices[0]['product_tax_id']
                            );
                            $lists['custom_tax'] = ShopFunctions::renderTaxList(
                                $vmdata->allPrices[0]['product_tax_id']
                            );
                        } else {
                            $lists['custom_tax'] = ShopFunctions::renderTaxList('');
                        }
                        if (isset($vmdata->allPrices[0]['product_discount_id'])) {
                            $calc_dname=$model->getDiscountCalculationName(
                                $vmdata->allPrices[0]['product_discount_id']
                            );
                            $lists['custom_discount'] = $this->renderDiscountList(
                                $data[0]->product_discount_id
                            );
                        } else {
                            $lists['custom_discount'] =$this->renderDiscountList('');
                        }
                        if (isset($vmdata->virtuemart_manufacturer_id)
                            && count($vmdata->virtuemart_manufacturer_id)
                        ) {
                            $lists['manufacturers'] 
                                = shopFunctions::renderManufacturerList(
                                    $vmdata->virtuemart_manufacturer_id,
                                    true
                                );
                        } else {
                            $lists['manufacturers']
                                = shopFunctions::renderManufacturerList(
                                    0,
                                    true
                                );
                        }

                    }
                } else {
                    $lists['custom_tax'] = ShopFunctions::renderTaxList('');
                    $lists['custom_discount'] = $this->renderDiscountList('');
                    $lists['manufacturers'] = shopFunctions::renderManufacturerList(
                        0,
                        true
                    );
                }
                $this->assignRef('data', $data);
                $this->assignRef('lists', $lists);
                $this->assignRef('images', $images);
                if (!class_exists('ShopFunctions')) {
                    include JPATH_VM_ADMINISTRATOR.
                    DIRECTORY_SEPARATOR.'helpers'.DIRECTORY_SEPARATOR.
                    'shopfunctions.php';
                }
                $active_languages=VmConfig::get('active_languages');
                $this->assignRef('active_lang', $active_languages);
                JPluginHelper::importPlugin('system');
                $dispatcher =JDispatcher::getInstance();
                $addonData = $dispatcher->trigger(
                    'onSellerAddProductView',
                    array($this->virtuemart_product_id,$data)
                );
                $this->assignRef('addonData', $addonData);
                $jInput=JFactory::getApplication()->input;
                $virtuemartProductId=$jInput->get(
                    'virtuemart_product_id',
                    '',
                    'INT'
                );
                if (JString::strlen($virtuemartProductId)
                    && is_numeric($virtuemartProductId)
                ) {
                    $createdBy=$model->checkSellerProduct($virtuemartProductId);
                    if (!isset($createdBy) || !$createdBy==JFactory::getUser()->id) {
                        JFactory::getApplication()->enqueueMessage(
                            JText::_('Product Id Not Found'),
                            'error'
                        );
                    } else {
                        parent::display($tpl);
                    }
                } else {
                    parent::display($tpl);
                }
                
            } else {
                JFactory::getApplication()->enqueueMessage(
                    JText::_('NOT_LOGIN_WITH_SELLER_ACCOUNT'),
                    'error'
                );
            }
        } else {
            JFactory::getApplication()->enqueueMessage(
                JText::_('PLZ_LOG_SEE_ADD_PROD'),
                'error'
            );
        }
    }
    /**
     * This is renderDiscountList function
     *
     * @param [type] $selected var
     * @param string $name     var
     * 
     * @return void
     */
    public function renderDiscountList($selected,$name='product_discount_id')
    {
        if (!class_exists('VirtueMartModelCalc')) {
            include VMPATH_ADMIN.DIRECTORY_SEPARATOR.
            'models'.DIRECTORY_SEPARATOR.'calc.php';
        }
        $discounts = VirtueMartModelCalc::getDiscounts();
        $discountrates = array();
        $discountrates[] = JHtml::_(
            'select.option',
            '-1',
            vmText::_('COM_VIRTUEMART_PRODUCT_DISCOUNT_NONE'),
            'product_discount_id'
        );
        $discountrates[] = JHtml::_(
            'select.option',
            '0',
            vmText::_('COM_VIRTUEMART_PRODUCT_DISCOUNT_NO_SPECIAL'),
            'product_discount_id'
        );
        foreach ($discounts as $discount) {
            $discountrates[] = JHtml::_(
                'select.option',
                $discount->virtuemart_calc_id,
                $discount->calc_name,
                'product_discount_id'
            );
        }
        $listHTML = JHtml::_(
            'Select.genericlist',
            $discountrates,
            $name,
            '',
            'product_discount_id',
            'text',
            $selected
        );
        return $listHTML;
    }
    /**
     * This is categoryListTree function
     *
     * @param array   $selectedCategories var
     * @param integer $cid                var
     * @param integer $level              var
     * @param array   $disabledFields     var
     * 
     * @return void
     */
    static public function categoryListTree(
        $selectedCategories = array(),
        $cid = 0,
        $level = 0,
        $disabledFields = array()
    ) {
        $hash = crc32(
            implode('.', $selectedCategories).':'.
            $cid.':'.$level.implode('.', $disabledFields)
        );
        $vendorId=true;
        if (empty(ShopFunctions::$categoryTree[$hash])) {
            $app = JFactory::getApplication();
            $cache = JFactory::getCache('com_virtuemart_cats');
            $cache->setCaching(1);
            ShopFunctions::$categoryTree[$hash] = $cache->call(
                array(
                    'ShopFunctions',
                    'categoryListTreeLoop'
                ),
                $selectedCategories,
                $cid,
                $level,
                $disabledFields,
                $app->isSite(),
                $vendorId,
                VmConfig::$vmlang
            );
            ShopFunctions::$categoryTree[$hash]
                = ShopFunctions::categoryListTreeLoop(
                    $selectedCategories,
                    $cid,
                    $level,
                    $disabledFields,
                    $app->isSite(),
                    true,
                    VmConfig::$vmlang
                );
        }
        return ShopFunctions::$categoryTree[$hash];
    }
}
